<?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('documents', function (Blueprint $table) {
            $table->id();

            // Relasi ke business trip dan attendance (opsional)
            $table->foreignId('business_trip_id')
                ->nullable()
                ->constrained()
                ->cascadeOnDelete();

            $table->foreignId('business_trip_attendance_id')
                ->nullable()
                ->constrained()
                ->cascadeOnDelete();

            // Siapa yang upload
            $table->foreignId('user_id')
                ->constrained()
                ->cascadeOnDelete();

            // Tipe dokumen
            $table->enum('type', [
                'tiket_pesawat',
                'tiket_kereta',
                'tiket_bus',
                'boarding_pass',
                'hotel_invoice',
                'kwitansi_bbm',
                'toll_receipt',
                'surat_tugas',
                'lainnya',
            ]);

            // Info file
            $table->string('file_name');
            $table->string('file_path');
            $table->string('file_type')->nullable(); // mime type
            $table->unsignedBigInteger('file_size')->nullable(); // dalam bytes
            $table->text('description')->nullable();

            // Status approval
            $table->enum('status', ['pending', 'approved', 'rejected'])
                ->default('pending');

            // Approval info
            $table->foreignId('approved_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->timestamp('approved_at')->nullable();
            $table->text('rejection_reason')->nullable();

            // Status hardcopy received
            $table->boolean('hardcopy_received')->default(false);
            $table->foreignId('hardcopy_received_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('hardcopy_received_at')->nullable();

            $table->timestamps();

            // Index
            $table->index(['business_trip_id', 'status']);
            $table->index(['user_id', 'status']);
            $table->index('type');
        });
    }

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