<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('kasbons', function (Blueprint $table) {
            $table->id('id_kasbon');
            
            // ===================================
            // IDENTITAS
            // ===================================
            $table->unsignedBigInteger('id_teknisi')
                  ->comment('ID teknisi yang ngasbon');
            
            // ===================================
            // DETAIL KASBON
            // ===================================
            $table->decimal('jumlah_kasbon', 12, 2)
                  ->comment('Jumlah uang yang dikasbon');
            
            $table->date('tanggal_kasbon')
                  ->comment('Tanggal kasbon diambil');
            
            $table->integer('periode_bulan')
                  ->comment('Bulan periode kasbon (1-12)')
                  ->check('periode_bulan >= 1 AND periode_bulan <= 12');
            
            $table->year('periode_tahun')
                  ->comment('Tahun periode kasbon');
            
            // ===================================
            // KETERANGAN (OPSIONAL)
            // ===================================
            $table->string('keperluan', 100)
                  ->nullable()
                  ->comment('Keperluan kasbon (dropdown: biaya sekolah, obat, dll). OPSIONAL');
            
            $table->text('keterangan_detail')
                  ->nullable()
                  ->comment('Keterangan tambahan jika perlu. OPSIONAL');
            
            // ===================================
            // PEMBERIAN KASBON (Oleh Mandor)
            // ===================================
            $table->enum('metode_pemberian', ['cash', 'transfer'])
                  ->default('cash')
                  ->comment('Metode pemberian kasbon oleh mandor');
            
            // ===================================
            // BUKTI KASBON (OPSIONAL)
            // ===================================
            $table->text('bukti_kasbon')
                  ->nullable()
                  ->comment('Path foto buku kasbon. OPSIONAL - bisa NULL untuk fase awal');
            
            // ===================================
            // TRACKING (Untuk Audit)
            // ===================================
            $table->unsignedBigInteger('diinput_oleh')
                  ->nullable()
                  ->comment('ID user/mandor yang input kasbon ini');
            
            $table->dateTime('waktu_input')
                  ->default(DB::raw('CURRENT_TIMESTAMP'))
                  ->comment('Waktu kasbon diinput ke sistem (untuk audit)');
            
            // ===================================
            // STATUS & PELUNASAN
            // ===================================
            $table->enum('status', ['belum_lunas', 'lunas'])
                  ->default('belum_lunas')
                  ->comment('Status pelunasan kasbon');
            
            $table->dateTime('tanggal_dilunasi')
                  ->nullable()
                  ->comment('Tanggal kasbon dilunasi (dipotong dari gaji)');
            
            $table->unsignedBigInteger('id_penggajian')
                  ->nullable()
                  ->comment('ID penggajian yang memotong kasbon ini');
            
            $table->text('catatan_pelunasan')
                  ->nullable()
                  ->comment('Catatan saat pelunasan');
            
            $table->timestamps();
            
            // ===================================
            // FOREIGN KEYS
            // ===================================
            $table->foreign('id_teknisi')
                  ->references('id_teknisi')
                  ->on('teknisis')
                  ->onDelete('cascade');
            
            $table->foreign('id_penggajian')
                  ->references('id_penggajian')
                  ->on('penggajians')
                  ->onDelete('set null');
            
            // ✅ TAMBAHAN: Foreign key untuk diinput_oleh
            $table->foreign('diinput_oleh')
                  ->references('id')
                  ->on('users')
                  ->onDelete('set null');
            
            // ===================================
            // INDEXES
            // ===================================
            $table->index('tanggal_kasbon', 'idx_tgl_kasbon');
            $table->index('status', 'idx_status');
            $table->index(['periode_tahun', 'periode_bulan'], 'idx_periode');
            $table->index('id_penggajian', 'idx_penggajian');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kasbons');
    }
};