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

/**
 * Migration 001 — Admin users
 * Roles: super_admin (full access), editor (content only).
 */
class Migration_Create_users extends CI_Migration {

	public function up()
	{
		$this->dbforge->add_field(array(
			'id'         => array('type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE),
			'name'       => array('type' => 'VARCHAR', 'constraint' => 120),
			'email'      => array('type' => 'VARCHAR', 'constraint' => 160),
			'password'   => array('type' => 'VARCHAR', 'constraint' => 255),
			'role'       => array('type' => "ENUM('super_admin','editor')", 'default' => 'editor'),
			'avatar'     => array('type' => 'VARCHAR', 'constraint' => 255, 'null' => TRUE),
			'phone'      => array('type' => 'VARCHAR', 'constraint' => 30, 'null' => TRUE),
			'is_active'  => array('type' => 'TINYINT', 'constraint' => 1, 'default' => 1),
			'last_login' => array('type' => 'DATETIME', '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('email');
		$this->dbforge->create_table('users', TRUE, array('ENGINE' => 'InnoDB'));

		// Seed default super admin — password: BloomIris@2026
		$this->db->insert('users', array(
			'name'       => 'Bloom Iris Admin',
			'email'      => 'admin@bloomiris.com',
			'password'   => password_hash('BloomIris@2026', PASSWORD_BCRYPT),
			'role'       => 'super_admin',
			'is_active'  => 1,
			'created_at' => date('Y-m-d H:i:s'),
		));
	}

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