<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAttendanceRecordsTable extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'employee_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
            ],
            'shift_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
                'null'     => true,
            ],
            'attendance_date' => [
                'type' => 'DATE',
            ],
            'clock_in' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'clock_out' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['present', 'absent', 'late', 'half_day', 'on_leave', 'holiday', 'auto_present'],
                'default'    => 'absent',
            ],
            'late_minutes' => [
                'type'       => 'SMALLINT',
                'unsigned'   => true,
                'default'    => 0,
            ],
            'early_exit_minutes' => [
                'type'       => 'SMALLINT',
                'unsigned'   => true,
                'default'    => 0,
            ],
            'overtime_minutes' => [
                'type'       => 'SMALLINT',
                'unsigned'   => true,
                'default'    => 0,
            ],
            'work_minutes' => [
                'type'       => 'SMALLINT',
                'unsigned'   => true,
                'default'    => 0,
            ],
            'source' => [
                'type'       => 'ENUM',
                'constraint' => ['clock', 'manual', 'auto', 'correction'],
                'default'    => 'clock',
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_by' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
                'null'     => true,
            ],
            'updated_by' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
                'null'     => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['employee_id', 'attendance_date']);
        $this->forge->addKey('attendance_date');
        $this->forge->addKey('status');
        $this->forge->addKey('shift_id');

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

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