<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('attendance_punches')) {
            Schema::table('attendance_punches', function (Blueprint $table) {
                if (! Schema::hasColumn('attendance_punches', 'processed_type')) {
                    $table->string('processed_type', 32)->nullable()->after('status')->index();
                }
                if (! Schema::hasColumn('attendance_punches', 'is_duplicate')) {
                    $table->boolean('is_duplicate')->default(false)->after('processed_type');
                }
                if (! Schema::hasColumn('attendance_punches', 'source')) {
                    $table->string('source', 32)->default('BIOMETRIC_DEVICE')->after('is_duplicate');
                }
                if (! Schema::hasColumn('attendance_punches', 'device_transaction_id')) {
                    $table->string('device_transaction_id', 191)->nullable()->after('source');
                }
                if (! Schema::hasColumn('attendance_punches', 'attendance_date')) {
                    $table->date('attendance_date')->nullable()->after('punch_date')->index();
                }
            });

            // Partial uniqueness: only when device_transaction_id is present.
            // MySQL allows multiple NULLs under a unique index.
            Schema::table('attendance_punches', function (Blueprint $table) {
                $table->unique(
                    ['biometric_device_id', 'device_transaction_id'],
                    'attendance_punches_device_txn_unique'
                );
            });
        }

        if (Schema::hasTable('attendances') && ! Schema::hasColumn('attendances', 'manual_time_lock')) {
            Schema::table('attendances', function (Blueprint $table) {
                $table->boolean('manual_time_lock')->default(false)->after('status');
            });
        }

        if (Schema::hasTable('attendance_policies')) {
            Schema::table('attendance_policies', function (Blueprint $table) {
                if (! Schema::hasColumn('attendance_policies', 'biometric_attendance_mode')) {
                    $table->string('biometric_attendance_mode', 32)
                        ->default('FIRST_IN_LAST_OUT')
                        ->after('allow_mobile_checkin');
                }
                if (! Schema::hasColumn('attendance_policies', 'biometric_min_punch_gap_minutes')) {
                    $table->unsignedSmallInteger('biometric_min_punch_gap_minutes')
                        ->default(1)
                        ->after('biometric_attendance_mode');
                }
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('attendance_punches')) {
            Schema::table('attendance_punches', function (Blueprint $table) {
                $table->dropUnique('attendance_punches_device_txn_unique');
            });

            Schema::table('attendance_punches', function (Blueprint $table) {
                $cols = [];
                foreach (['processed_type', 'is_duplicate', 'source', 'device_transaction_id', 'attendance_date'] as $col) {
                    if (Schema::hasColumn('attendance_punches', $col)) {
                        $cols[] = $col;
                    }
                }
                if ($cols !== []) {
                    $table->dropColumn($cols);
                }
            });
        }

        if (Schema::hasTable('attendances') && Schema::hasColumn('attendances', 'manual_time_lock')) {
            Schema::table('attendances', function (Blueprint $table) {
                $table->dropColumn('manual_time_lock');
            });
        }

        if (Schema::hasTable('attendance_policies')) {
            Schema::table('attendance_policies', function (Blueprint $table) {
                $cols = [];
                foreach (['biometric_attendance_mode', 'biometric_min_punch_gap_minutes'] as $col) {
                    if (Schema::hasColumn('attendance_policies', $col)) {
                        $cols[] = $col;
                    }
                }
                if ($cols !== []) {
                    $table->dropColumn($cols);
                }
            });
        }
    }
};
