<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('penugasans', function (Blueprint $table) {
            $table->id('id_penugasan');

            // ===================================
            // INFO PENUGASAN DASAR (Input Mandor)
            // ===================================
            $table->unsignedBigInteger('id_teknisi')->comment('Teknisi utama yang ditugaskan');
            $table->text('foto_surat')->nullable()->comment('Path foto surat tugas dari kantor'); // ✅ BARU
            $table->date('tanggal_diberikan')->comment('Tanggal tugas diberikan');
            $table->text('catatan_admin')->nullable()->comment('Catatan/instruksi dari mandor');

            // ===================================
            // JENIS PEKERJAAN (Diisi Teknisi via Mobile)
            // ===================================
            $table->enum('jenis_pekerjaan', [
                'sr',
                'pengembangan_jaringan_pipa',
                'pengangkatan',
                'pemasangan_gate_valve',
                'gali_urug',
                'perbaikan_jaringan_pipa',
                'pengecatan_pipa_besi',
                'penyempurnaan_jaringan_pipa'
            ])->nullable()->comment('Jenis pekerjaan yang ditugaskan');

            // ===================================
            // DETAIL PEKERJAAN (Diisi Teknisi via Mobile) - SEMUA NULLABLE
            // ===================================
            $table->enum('dimensi_pipa', ['1-2', '3', '4', '6', '8', '10', '12'])
                  ->nullable()
                  ->comment('Dimensi pipa dalam inchi');

            $table->decimal('jarak_meter', 10, 2)
                  ->nullable()
                  ->comment('Panjang pipa dalam meter');

            $table->integer('jumlah_unit')
                  ->nullable()
                  ->comment('Jumlah unit. Untuk: SR, pengangkatan');

            $table->integer('jumlah_titik')
                  ->nullable()
                  ->comment('Jumlah titik galian. Untuk: gali urug');

            $table->boolean('pakai_pipa_besi')
                  ->nullable()
                  ->comment('Khusus pemasangan gate valve');

            $table->enum('jenis_pengangkatan', ['meteran', 'gate_valve'])
                  ->nullable()
                  ->comment('Khusus pengangkatan: meteran air atau gate valve terpendam');

            $table->text('detail_pekerjaan')
                  ->nullable()
                  ->comment('Catatan detail dari teknisi');

            // ===================================
            // ONGKOS PEKERJAAN
            // ===================================
            $table->unsignedBigInteger('id_tarif')
                  ->nullable()
                  ->comment('Referensi ke tabel tarif_pekerjaans');

            $table->decimal('total_nilai_pekerjaan', 15, 2)
                  ->nullable()
                  ->comment('Total ongkos pekerjaan kotor (belum dibagi tim)');

            // ===================================
            // STATUS & TRACKING
            // ===================================
            $table->enum('status_pekerjaan', [
                'belum_mulai',
                'dalam_proses',
                'selesai',
                'dibatalkan'
            ])->default('belum_mulai');

            $table->dateTime('tanggal_mulai')->nullable()->comment('Kapan teknisi mulai kerja');
            $table->dateTime('tanggal_diselesaikan')->nullable()->comment('Kapan pekerjaan selesai');

            // ===================================
            // GARANSI (Khusus SR - 3 bulan)
            // ===================================
            $table->date('tanggal_garansi_mulai')->nullable();
            $table->date('tanggal_garansi_selesai')->nullable();
            $table->text('catatan_garansi')->nullable();

            // ===================================
            // FOTO BUKTI PEKERJAAN (dari teknisi via mobile)
            // ===================================
            $table->text('foto_sebelum')->nullable()->comment('Path foto sebelum pekerjaan');
            $table->text('foto_sesudah')->nullable()->comment('Path foto setelah pekerjaan');

            $table->timestamps();
            $table->softDeletes();

            // ===================================
            // FOREIGN KEYS
            // ===================================
            $table->foreign('id_teknisi')
                  ->references('id_teknisi')
                  ->on('teknisis')
                  ->onDelete('cascade');

            $table->foreign('id_tarif')
                  ->references('id_tarif')
                  ->on('tarif_pekerjaans')
                  ->onDelete('set null');
        });
    }

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