<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAgencies extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                   => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'company_name'         => ['type' => 'VARCHAR', 'constraint' => 150],
            'slug'                 => ['type' => 'VARCHAR', 'constraint' => 160],
            'logo_path'            => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'website'              => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'email'                => ['type' => 'VARCHAR', 'constraint' => 150],
            'phone'                => ['type' => 'VARCHAR', 'constraint' => 30, 'null' => true],
            'industry'             => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'company_size'         => ['type' => 'VARCHAR', 'constraint' => 30, 'null' => true],
            'description'          => ['type' => 'TEXT', 'null' => true],
            'registration_number'  => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'tax_number'           => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'address'              => ['type' => 'TEXT', 'null' => true],
            'verification_status'  => ['type' => 'ENUM', 'constraint' => ['pending', 'under_review', 'verified', 'rejected'], 'default' => 'pending'],
            'status'               => ['type' => 'ENUM', 'constraint' => ['active', 'suspended', 'trial_expired', 'cancelled'], 'default' => 'active'],
            'owner_user_id'        => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'timezone'             => ['type' => 'VARCHAR', 'constraint' => 50, 'default' => 'Asia/Karachi'],
            'created_at'           => ['type' => 'TIMESTAMP', 'null' => true],
            'updated_at'           => ['type' => 'TIMESTAMP', 'null' => true],
            'deleted_at'           => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('slug');
        $this->forge->addKey('status');
        $this->forge->addKey('verification_status');
        $this->forge->createTable('agencies', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'            => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'agency_id'     => ['type' => 'BIGINT', 'unsigned' => true],
            'doc_type'      => ['type' => 'ENUM', 'constraint' => ['business_registration', 'tax_document', 'utility_bill', 'other']],
            'file_path'     => ['type' => 'VARCHAR', 'constraint' => 255],
            'status'        => ['type' => 'ENUM', 'constraint' => ['pending', 'approved', 'rejected'], 'default' => 'pending'],
            'reviewed_by'   => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'reviewed_at'   => ['type' => 'TIMESTAMP', 'null' => true],
            'created_at'    => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('agency_id', 'agencies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('agency_verification_documents', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('agency_verification_documents', true);
        $this->forge->dropTable('agencies', true);
    }
}
