<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateServicesTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'company_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'client_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'type' => ['type' => 'ENUM', 'constraint' => ['hosting', 'domain', 'other']],
            'name' => ['type' => 'VARCHAR', 'constraint' => 255],
            'price' => ['type' => 'DECIMAL', 'constraint' => '12,2'],
            'currency' => ['type' => 'VARCHAR', 'constraint' => 3, 'default' => 'USD'],
            'billing_cycle' => ['type' => 'ENUM', 'constraint' => ['monthly', 'quarterly', 'semi_annual', 'annual'], 'default' => 'annual'],
            'start_date' => ['type' => 'DATE'],
            'renewal_date' => ['type' => 'DATE'],
            'status' => ['type' => 'ENUM', 'constraint' => ['active', 'expired', 'cancelled', 'suspended'], 'default' => 'active'],
            'notes' => ['type' => 'TEXT', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('renewal_date');
        $this->forge->addForeignKey('company_id', 'companies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('client_id', 'clients', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('services');
    }

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