<?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('business_trip_attendances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_trip_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->date('date');

            // Tipe attendance untuk perjalanan dinas
            // departure: Keberangkatan dari rumah (hari pertama pagi)
            // arrival: Kedatangan di kota tujuan (hari pertama malam)
            // daily: Presensi harian selama di kota tujuan
            // return_departure: Keberangkatan pulang dari kota tujuan
            // return_arrival: Sampai di rumah
            $table->enum('type', ['departure', 'arrival', 'daily', 'return_departure', 'return_arrival'])->default('daily');

            // Check In
            $table->timestamp('check_in_time')->nullable();
            $table->decimal('check_in_latitude', 10, 8)->nullable();
            $table->decimal('check_in_longitude', 11, 8)->nullable();
            $table->text('check_in_address')->nullable();
            $table->string('check_in_city')->nullable();
            $table->string('check_in_province')->nullable();
            $table->boolean('check_in_is_editable')->default(false); // apakah lokasi diedit manual di map
            $table->text('check_in_notes')->nullable();

            // Check Out
            $table->timestamp('check_out_time')->nullable();
            $table->decimal('check_out_latitude', 10, 8)->nullable();
            $table->decimal('check_out_longitude', 11, 8)->nullable();
            $table->text('check_out_address')->nullable();
            $table->string('check_out_city')->nullable();
            $table->string('check_out_province')->nullable();
            $table->boolean('check_out_is_editable')->default(false);
            $table->text('check_out_notes')->nullable();

            // Referensi lokasi rekomendasi (BPKP/Pemda)
            $table->foreignId('reference_office_id')->nullable()->constrained('offices')->nullOnDelete();
            $table->integer('distance_from_reference')->nullable(); // meter

            // Status klaim untuk approval
            // draft: belum diklaim
            // claimed: sudah diklaim user, menunggu approval admin
            // approved: sudah diapprove admin
            // rejected: ditolak admin
            // auto_approved: otomatis di-acc (bypass enabled)
            $table->enum('claim_status', ['draft', 'claimed', 'approved', 'rejected', 'auto_approved'])->default('draft');
            $table->timestamp('claimed_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->text('rejection_reason')->nullable();

            $table->timestamps();

            $table->unique(['business_trip_id', 'date', 'type']);
            $table->index(['user_id', 'date']);
            $table->index(['claim_status']);
        });
    }

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