<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePlanChangeRequests extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'agency_id'          => ['type' => 'BIGINT', 'unsigned' => true],
            'requested_by_user_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'current_plan_id'    => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'requested_plan_id'  => ['type' => 'BIGINT', 'unsigned' => true],
            'billing_cycle'      => ['type' => 'ENUM', 'constraint' => ['monthly', 'yearly'], 'default' => 'monthly'],
            'status'             => ['type' => 'ENUM', 'constraint' => ['pending', 'approved', 'rejected'], 'default' => 'pending'],
            'note'               => ['type' => 'TEXT', 'null' => true],
            'reviewed_by_user_id' => ['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->addKey(['agency_id', 'status']);
        $this->forge->addKey('status');
        $this->forge->addForeignKey('agency_id', 'agencies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('requested_plan_id', 'subscription_plans', 'id', false, 'RESTRICT');
        $this->forge->createTable('plan_change_requests', true, ['ENGINE' => 'InnoDB']);
    }

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