<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * office_time_enabled: true = menggunakan sistem shift/waktu kantor, false = menggunakan aturan jam 9 (auto normalize)
     * Jika office_time_enabled = true:
     *   - Perlu ada shift yang di-assign ke employee
     *   - Keterlambatan dihitung berdasarkan jam masuk shift
     * Jika office_time_enabled = false:
     *   - Tidak perlu shift
     *   - Jika checkin > jam 9, sistem auto normalize ke jam 8 (jika attendance_auto_enabled)
     */
    public function up(): void
    {
        Schema::table('companies', function (Blueprint $table) {
            // Setting untuk mengaktifkan waktu kantor / shift
            $table->boolean('office_time_enabled')->default(false)->after('attendance_auto_enabled');
            // Toleransi keterlambatan dalam menit (misal 15 menit)
            $table->integer('late_tolerance_minutes')->default(15)->after('office_time_enabled');
        });

        // Tabel shifts untuk menyimpan jam kerja
        Schema::create('shifts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->onDelete('cascade');
            $table->string('name'); // contoh: "Shift Pagi", "Shift Siang", "Normal"
            $table->string('code')->nullable(); // contoh: "SP", "SS", "N"
            $table->time('start_time'); // jam masuk
            $table->time('end_time'); // jam pulang
            $table->boolean('is_default')->default(false); // shift default untuk company
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['company_id', 'code']);
        });

        // Tabel pivot untuk assign shift ke user
        Schema::create('user_shifts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('shift_id')->constrained()->onDelete('cascade');
            $table->date('effective_date'); // tanggal mulai berlaku shift ini
            $table->date('end_date')->nullable(); // tanggal berakhir (null = tidak ada batas)
            $table->timestamps();

            $table->index(['user_id', 'effective_date']);
        });

        // Tambah kolom shift_id ke attendances untuk tracking shift yang digunakan saat checkin
        Schema::table('attendances', function (Blueprint $table) {
            $table->foreignId('shift_id')->nullable()->after('business_trip_location_id')->constrained()->onDelete('set null');
            $table->integer('late_minutes')->default(0)->after('status'); // menit keterlambatan
            $table->integer('early_leave_minutes')->default(0)->after('late_minutes'); // menit pulang lebih awal
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('attendances', function (Blueprint $table) {
            $table->dropForeign(['shift_id']);
            $table->dropColumn(['shift_id', 'late_minutes', 'early_leave_minutes']);
        });

        Schema::dropIfExists('user_shifts');
        Schema::dropIfExists('shifts');

        Schema::table('companies', function (Blueprint $table) {
            $table->dropColumn(['office_time_enabled', 'late_tolerance_minutes']);
        });
    }
};
