<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('attendance_settings', function (Blueprint $table) {
            $table->id();

            // Basic Info
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->string('name', 100);
            $table->text('description')->nullable();

            // Source Configuration
            $table->enum('source_type', ['CSV', 'API', 'BOTH'])->default('CSV');
            $table->enum('punch_mode', ['SINGLE', 'IN_OUT'])->default('IN_OUT');

            // Format Configuration
            $table->string('date_format', 50)->default('Y-m-d');
            $table->string('time_format', 50)->default('H:i:s');
            $table->string('datetime_format', 50)->nullable();
            $table->string('timezone', 50)->default('UTC');

            // CSV-Specific Settings
            $table->char('csv_delimiter', 1)->default(',');
            $table->boolean('csv_has_header')->default(true);
            $table->string('csv_encoding', 20)->default('UTF-8');

            // Field Mapping Configuration (JSON)
            $table->json('field_mapping');

            // IN/OUT Value Mapping (JSON)
            $table->json('in_out_mapping')->nullable();

            // Control Flags
            $table->boolean('is_default')->default(false);
            $table->boolean('status')->default(true);

            $table->timestamps();

            // Indexes
            $table->index(['branch_id', 'is_default']);
            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('attendance_settings');
    }
};
