<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Unified locations table - menggabungkan offices dan business_trip_locations
     */
    public function up(): void
    {
        Schema::create('locations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->onDelete('cascade');

            // Identifikasi
            $table->string('name');
            $table->string('code', 50)->nullable();

            // Alamat
            $table->text('address')->nullable();
            $table->string('city', 100)->nullable();
            $table->string('province', 100)->nullable();

            // Geolocation
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->integer('radius')->default(100); // dalam meter

            // Kategorisasi
            // internal: lokasi milik perusahaan (headquarters, branch)
            // external: lokasi eksternal (bpkp, pemda, kementerian, lembaga, dll)
            $table->enum('category', ['internal', 'external'])->default('internal');
            $table->enum('type', [
                'headquarters',  // Kantor pusat
                'branch',        // Kantor cabang
                'bpkp',          // BPKP
                'pemda',         // Pemerintah Daerah
                'kementerian',   // Kementerian
                'lembaga',       // Lembaga pemerintah lainnya
                'other'          // Lainnya
            ])->default('other');

            // Penggunaan/Permission
            $table->boolean('allow_daily_checkin')->default(false);  // Untuk presensi harian
            $table->boolean('allow_business_trip')->default(true);   // Untuk tujuan dinas

            // Status
            $table->boolean('is_active')->default(true);

            $table->timestamps();

            // Indexes untuk performa query
            $table->index(['company_id', 'category']);
            $table->index(['company_id', 'type']);
            $table->index(['company_id', 'allow_daily_checkin']);
            $table->index(['company_id', 'allow_business_trip']);
            $table->index(['company_id', 'is_active']);
            $table->index(['latitude', 'longitude']);
        });
    }

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