<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateLeaveBalancesTable extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'employee_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
            ],
            'leave_type_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
            ],
            'year' => [
                'type'       => 'SMALLINT',
                'unsigned'   => true,
            ],
            'entitled' => [
                'type'       => 'DECIMAL',
                'constraint' => '5,1',
                'default'    => 0,
            ],
            'used' => [
                'type'       => 'DECIMAL',
                'constraint' => '5,1',
                'default'    => 0,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['employee_id', 'leave_type_id', 'year']);
        $this->forge->addKey('employee_id');
        $this->forge->addKey('leave_type_id');
        $this->forge->addKey('year');

        $this->forge->createTable('leave_balances', true, ['ENGINE' => 'InnoDB']);
    }

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