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

            $table->bigIncrements('PV_Id');

            $table->enum('PV_Ty', ['VEND', 'GRNT'])->default('VEND');

            $table->string('PV_No', 50)->nullable();

            $table->date('PV_Chln_Date');

            $table->unsignedBigInteger('Dpt_Id');
            $table->unsignedBigInteger('Lg_Id');
            $table->unsignedBigInteger('SL_Id');
            $table->unsignedBigInteger('WA_Id')->nullable();

            $table->decimal('Gross_Amt', 15, 2)->nullable();
            $table->decimal('Add_Amt', 15, 2)->nullable();
            $table->decimal('Less_Amt', 15, 2)->default(0.00);
            $table->decimal('Net_Amt', 15, 2)->nullable();

            $table->enum('Status', [
                'draft',
                'pending',
                'approved',
                'paid',
                'cancelled',
                'quary',
                'hod_app',
                'hod_query',
                'acco_app',
                'acco_query'
            ])->default('draft');

            $table->timestamp('HOD_Approved')->nullable();
            $table->integer('HOD_Officer')->nullable();

            $table->timestamp('Acct_Approved')->nullable();
            $table->integer('Acct_Officer')->nullable();

            $table->text('Remarks')->nullable();

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


            // ================= FK =================

            $table->foreign('Dpt_Id')
                ->references('Dpt_id')
                ->on('dept_master')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('Lg_Id')
                ->references('Lg_Id')
                ->on('ledger_master')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('SL_Id')
                ->references('SL_Id')
                ->on('subledg_master')
                ->onDelete('restrict')
                ->onUpdate('cascade');

            $table->foreign('WA_Id')
                ->references('WA_Id')
                ->on('work_allocation_book')
                ->onDelete('restrict')
                ->onUpdate('cascade');
        });
    }

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