<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateCompanyDept extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'dept_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'company_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'parent_dept_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'default' => 0,
            ],
            'department_code' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
            ],
            'department_name' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
            ],
            
            // Common fields
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'created_by' => [
                'type' => 'VARCHAR', 
                'constraint' => '20', 
                'null' => true
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_by' => [
                'type' => 'VARCHAR', 
                'constraint' => '20', 
                'null' => true
            ],
            'deleted_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('dept_id', true);
        $this->forge->addForeignKey('company_id', 'company_info', 'company_id', 'CASCADE', 'RESTRICT');
        $this->forge->createTable('company_dept');
    }

    public function down()
    {
        $this->forge->dropTable('company_dept');
    }
}
