<?php

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

class CreateOdooConfigTables extends Migration
{
    public function up()
    {
        // ── 1. Systems ────────────────────────────────────────────────────────
        if (!Schema::hasTable('odoo_systems')) {
            Schema::create('odoo_systems', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');                       // e.g. "DA", "Credit Fair", "Co-lending"
                $table->string('slug')->unique();             // e.g. "da", "credit-fair", "co-lending"
                $table->text('description')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }

        // ── 2. Sections ───────────────────────────────────────────────────────
        if (!Schema::hasTable('odoo_jv_sections')) {
            Schema::create('odoo_jv_sections', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('system_id');
                $table->string('name');                       // e.g. "Disbursement", "Interest"
                $table->unsignedInteger('sort_order')->default(0);
                $table->timestamps();

                $table->foreign('system_id')
                      ->references('id')->on('odoo_systems')
                      ->onDelete('cascade');
            });
        }

        // ── 3. Steps ──────────────────────────────────────────────────────────
        if (!Schema::hasTable('odoo_jv_steps')) {
            Schema::create('odoo_jv_steps', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('section_id');
                $table->string('step_key');                   // e.g. "disbursement_pv"
                $table->string('label')->nullable();          // human-readable label
                $table->string('voucher_type')->nullable();   // e.g. "Payment Voucher"
                $table->boolean('is_enabled')->default(true);
                $table->unsignedInteger('sort_order')->default(0);
                $table->timestamps();

                $table->foreign('section_id')
                      ->references('id')->on('odoo_jv_sections')
                      ->onDelete('cascade');
            });
        }

        // ── 4. Entries ────────────────────────────────────────────────────────
        if (!Schema::hasTable('odoo_jv_entries')) {
            Schema::create('odoo_jv_entries', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('step_id');
                $table->enum('type', ['Dr', 'Cr'])->default('Dr');
                $table->string('account')->nullable();        // e.g. "Loans and Advances"
                $table->string('gl_code')->nullable();        // e.g. "133001"
                $table->unsignedInteger('sequence')->default(1);
                $table->string('summary')->nullable();        // e.g. "Disbursed Amount"
                $table->string('amount_field')->nullable();   // free-text field name e.g. "disbursement_amount"
                $table->boolean('is_balancing_credit')->default(false); // accumulates all debits as credit
                $table->timestamps();

                $table->foreign('step_id')
                      ->references('id')->on('odoo_jv_steps')
                      ->onDelete('cascade');
            });
        }
    }

    public function down()
    {
        Schema::dropIfExists('odoo_jv_entries');
        Schema::dropIfExists('odoo_jv_steps');
        Schema::dropIfExists('odoo_jv_sections');
        Schema::dropIfExists('odoo_systems');
    }
}
