<?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('cms_settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->string('type')->default('text');
            $table->string('group')->default('general');
            $table->timestamps();
        });

        Schema::create('leasing_data_uploads', function (Blueprint $table) {
            $table->id();
            $table->foreignId('leasing_profile_id')->constrained('leasing_profiles')->onDelete('cascade');
            $table->string('filename');
            $table->integer('row_count')->default(0);
            $table->string('status')->default('processing');
            $table->timestamps();
        });

        Schema::create('leasing_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('leasing_profile_id')->constrained('leasing_profiles')->onDelete('cascade');
            $table->foreignId('branch_id')->nullable()->constrained('branches')->onDelete('set null');
            $table->foreignId('upload_id')->nullable()->constrained('leasing_data_uploads')->onDelete('set null');
            $table->date('tgl_data');
            $table->string('nopol')->index();
            $table->string('rangka')->index();
            $table->string('mesin')->index();
            $table->string('model')->nullable();
            $table->string('kendaraan')->nullable();
            $table->string('customer_name')->nullable();
            $table->string('customer_phone')->nullable();
            $table->text('customer_address')->nullable();
            $table->integer('overdue_days')->default(0);
            $table->decimal('sisa_saldo', 15, 2)->default(0);
            $table->boolean('legal_flag')->default(false);
            $table->text('member_warning')->nullable();
            $table->text('member_instruction')->nullable();
            $table->text('internal_note')->nullable();
            $table->string('record_status')->default('active');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('leasing_records');
        Schema::dropIfExists('leasing_data_uploads');
        Schema::dropIfExists('cms_settings');
    }
};
