<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Migration 013 — Blog / insights
 * status: draft | published | scheduled (publish_at controls scheduling).
 */
class Migration_Create_blog extends CI_Migration {

	public function up()
	{
		$this->dbforge->add_field(array(
			'id'          => array('type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE),
			'slug'        => array('type' => 'VARCHAR', 'constraint' => 200),
			'title'       => array('type' => 'VARCHAR', 'constraint' => 200),
			'category'    => array('type' => 'VARCHAR', 'constraint' => 80, 'null' => TRUE),
			'cover_image' => array('type' => 'VARCHAR', 'constraint' => 255, 'null' => TRUE),
			'excerpt'     => array('type' => 'TEXT', 'null' => TRUE),
			'body'        => array('type' => 'LONGTEXT', 'null' => TRUE),
			'author_id'   => array('type' => 'INT', 'constraint' => 11, 'null' => TRUE),
			'status'      => array('type' => "ENUM('draft','published','scheduled')", 'default' => 'draft'),
			'publish_at'  => array('type' => 'DATETIME', 'null' => TRUE),
			'meta_title'  => array('type' => 'VARCHAR', 'constraint' => 200, 'null' => TRUE),
			'meta_desc'   => array('type' => 'VARCHAR', 'constraint' => 300, 'null' => TRUE),
			'views'       => array('type' => 'INT', 'constraint' => 8, 'default' => 0),
			'created_at'  => array('type' => 'DATETIME', 'null' => TRUE),
			'updated_at'  => array('type' => 'DATETIME', 'null' => TRUE),
		));
		$this->dbforge->add_key('id', TRUE);
		$this->dbforge->add_key('slug');
		$this->dbforge->add_key('status');
		$this->dbforge->create_table('blog', TRUE, array('ENGINE' => 'InnoDB'));

		$now = date('Y-m-d H:i:s');
		$this->db->insert('blog', array(
			'slug' => 'welcome-to-the-bloom-iris-journal',
			'title' => 'Welcome to the Bloom Iris Journal',
			'category' => 'Insights',
			'excerpt' => 'Behind the scenes of the creative work, talent and ideas shaping Bloom Iris.',
			'body' => '<p>This is the Bloom Iris Journal — a space for creative insights, campaign stories and behind-the-scenes moments.</p>',
			'author_id' => 1, 'status' => 'published', 'publish_at' => $now,
			'created_at' => $now,
		));
	}

	public function down()
	{
		$this->dbforge->drop_table('blog', TRUE);
	}
}
