<?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
    {
        // 1. Make expenses.title nullable so Flutter's description-only payload
        //    doesn't trigger "Field 'title' doesn't have a default value" errors.
        //    SyncController remaps Flutter's 'description' → 'title' before insert,
        //    but the nullable fallback prevents failures on older/partial payloads.
        Schema::table('expenses', function (Blueprint $table) {
            $table->string('title')->nullable()->default('')->change();
        });

        // 2. Add proof_image to user_subscriptions for crypto payment evidence.
        Schema::table('user_subscriptions', function (Blueprint $table) {
            $table->string('proof_image')->nullable()->after('admin_notes');
        });

        // 3. Add missing columns to customer_ledger that Flutter sends but server lacks.
        //    Flutter model uses 'description' (not 'notes') and 'customer_name'.
        Schema::table('customer_ledger', function (Blueprint $table) {
            $table->string('description')->nullable()->after('notes');
            $table->string('customer_name')->nullable()->after('customer_id');
        });

        // 4. Create customer_invoices table — a unified log of all customer-facing
        //    documents (POS receipts, sales invoices, purchase invoices, quotations).
        Schema::create('customer_invoices', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('store_id')->nullable();
            $table->string('local_id')->nullable();
            $table->string('customer_id')->nullable();
            $table->string('customer_name')->nullable();
            // invoice_type: pos_receipt | sales_invoice | purchase_invoice | quotation
            $table->string('invoice_type')->default('pos_receipt');
            $table->string('invoice_number')->nullable();
            // reference_id + reference_type link back to orders/purchases/quotations
            $table->string('reference_id')->nullable();
            $table->string('reference_type')->nullable();
            $table->decimal('total_amount', 14, 4)->default(0);
            $table->string('status')->default('issued');
            $table->timestamp('invoice_date')->nullable();
            $table->text('notes')->nullable();
            // Sync
            $table->string('device_id')->nullable();
            $table->bigInteger('sync_version')->default(0);
            $table->boolean('is_deleted')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index('user_id');
            $table->index(['user_id', 'customer_id']);
            $table->index(['user_id', 'invoice_type']);
            $table->index(['user_id', 'updated_at']);
        });
    }

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

        Schema::table('customer_ledger', function (Blueprint $table) {
            $table->dropColumn(['description', 'customer_name']);
        });

        Schema::table('user_subscriptions', function (Blueprint $table) {
            $table->dropColumn('proof_image');
        });

        Schema::table('expenses', function (Blueprint $table) {
            $table->string('title')->nullable(false)->default(null)->change();
        });
    }
};
