<?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('attendances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('office_id')->nullable()->constrained()->nullOnDelete();
            $table->date('date');
            $table->enum('type', ['regular', 'wfh', 'business_trip'])->default('regular');

            // 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->integer('check_in_distance')->nullable(); // jarak dari kantor dalam meter
            $table->text('check_in_address')->nullable();
            $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->integer('check_out_distance')->nullable();
            $table->text('check_out_address')->nullable();
            $table->text('check_out_notes')->nullable();

            // Status
            $table->enum('status', ['present', 'late', 'early_leave', 'absent', 'leave', 'sick'])->default('present');
            $table->integer('work_duration')->nullable(); // dalam menit
            $table->integer('overtime_duration')->nullable(); // dalam menit

            $table->timestamps();

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

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