<?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('collateral_categories', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
            $table->unique(['business_id', 'name']);
            $table->index(['business_id', 'status']);
        });

        Schema::create('collaterals', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('customer_id')->constrained()->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('category_id')->constrained('collateral_categories')->restrictOnDelete();
            $table->unsignedBigInteger('sequence_number');
            $table->string('code')->unique();
            $table->string('item');
            $table->string('brand')->nullable();
            $table->string('model')->nullable();
            $table->string('serial_number')->nullable();
            $table->string('colour')->nullable();
            $table->text('description')->nullable();
            $table->decimal('estimated_value', 18, 2)->nullable();
            $table->decimal('accepted_value', 18, 2)->nullable();
            $table->string('condition')->nullable();
            $table->date('received_at');
            $table->string('store_room')->nullable();
            $table->string('shelf')->nullable();
            $table->foreignId('received_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('status')->default('pending_inspection');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['branch_id', 'sequence_number']);
            $table->index(['branch_id', 'status']);
            $table->index(['customer_id', 'status']);
            $table->index('serial_number');
        });

        Schema::create('collateral_files', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collateral_id')->constrained()->restrictOnDelete();
            $table->string('kind');
            $table->string('document_type')->nullable();
            $table->string('original_name');
            $table->string('file_path');
            $table->string('mime_type')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['collateral_id', 'kind']);
        });

        Schema::create('collateral_movements', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collateral_id')->constrained()->restrictOnDelete();
            $table->string('action');
            $table->string('from_status')->nullable();
            $table->string('to_status')->nullable();
            $table->string('from_store_room')->nullable();
            $table->string('from_shelf')->nullable();
            $table->string('to_store_room')->nullable();
            $table->string('to_shelf')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('moved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('moved_at');
            $table->timestamps();
            $table->index(['collateral_id', 'moved_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('collateral_movements');
        Schema::dropIfExists('collateral_files');
        Schema::dropIfExists('collaterals');
        Schema::dropIfExists('collateral_categories');
    }
};
