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

/**
 * Migration 009 — Inquiries / leads
 * type: contact | brand  — both flow through the same pipeline.
 * Pipeline status: new -> reviewed -> converted -> closed
 */
class Migration_Create_inquiries extends CI_Migration {

	public function up()
	{
		$this->dbforge->add_field(array(
			'id'            => array('type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE),
			'type'          => array('type' => "ENUM('contact','brand')", 'default' => 'contact'),
			'name'          => array('type' => 'VARCHAR', 'constraint' => 140),
			'email'         => array('type' => 'VARCHAR', 'constraint' => 160),
			'phone'         => array('type' => 'VARCHAR', 'constraint' => 30, 'null' => TRUE),
			'company'       => array('type' => 'VARCHAR', 'constraint' => 160, 'null' => TRUE),
			'service_id'    => array('type' => 'INT', 'constraint' => 11, 'null' => TRUE),
			'budget_range'  => array('type' => 'VARCHAR', 'constraint' => 80, 'null' => TRUE),
			'timeline'      => array('type' => 'VARCHAR', 'constraint' => 80, 'null' => TRUE),
			'subject'       => array('type' => 'VARCHAR', 'constraint' => 200, 'null' => TRUE),
			'message'       => array('type' => 'TEXT', 'null' => TRUE),
			'brief_file'    => array('type' => 'VARCHAR', 'constraint' => 255, 'null' => TRUE),
			'status'        => array('type' => "ENUM('new','reviewed','converted','closed')", 'default' => 'new'),
			'admin_notes'   => array('type' => 'TEXT', 'null' => TRUE),
			'assigned_to'   => array('type' => 'INT', 'constraint' => 11, 'null' => TRUE),
			'ip_address'    => array('type' => 'VARCHAR', 'constraint' => 45, 'null' => TRUE),
			'created_at'    => array('type' => 'DATETIME', 'null' => TRUE),
			'updated_at'    => array('type' => 'DATETIME', 'null' => TRUE),
		));
		$this->dbforge->add_key('id', TRUE);
		$this->dbforge->add_key('status');
		$this->dbforge->add_key('type');
		$this->dbforge->create_table('inquiries', TRUE, array('ENGINE' => 'InnoDB'));

		// Newsletter subscribers
		$this->dbforge->add_field(array(
			'id'         => array('type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE),
			'email'      => array('type' => 'VARCHAR', 'constraint' => 160),
			'is_active'  => array('type' => 'TINYINT', 'constraint' => 1, 'default' => 1),
			'created_at' => array('type' => 'DATETIME', 'null' => TRUE),
		));
		$this->dbforge->add_key('id', TRUE);
		$this->dbforge->add_key('email');
		$this->dbforge->create_table('subscribers', TRUE, array('ENGINE' => 'InnoDB'));
	}

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