<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('support_invoices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('conversation_id')->nullable()->constrained('support_conversations')->onDelete('set null');
            $table->foreignId('created_by')->constrained('users')->onDelete('cascade'); // Admin who created it
            
            // Stripe details
            $table->string('stripe_invoice_id')->unique();
            $table->string('stripe_customer_id');
            $table->string('invoice_number')->nullable();
            
            // Invoice details
            $table->string('description');
            $table->decimal('amount', 10, 2);
            $table->string('currency', 3)->default('usd');
            $table->enum('status', ['draft', 'open', 'paid', 'void', 'uncollectible'])->default('open');
            
            // Payment details
            $table->string('hosted_invoice_url')->nullable(); // Stripe hosted URL
            $table->string('invoice_pdf')->nullable(); // PDF URL
            $table->timestamp('paid_at')->nullable();
            $table->string('payment_intent_id')->nullable();
            $table->text('metadata')->nullable(); // JSON metadata
            
            $table->timestamps();
            
            $table->index('user_id');
            $table->index('stripe_invoice_id');
            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('support_invoices');
    }
};
