<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the digital_machines table for managing health equipment marketing listings.
     * Supports multiple images, videos, price visibility control, and payment integration.
     *
     * Requirements: 3.1, 10.1, 10.2
     */
    public function up(): void
    {
        Schema::create('digital_machines', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to users (Health Marketor)
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('Owner/marketer of this machine listing');

            // Basic Information
            $table->string('name', 191)->comment('Machine name');
            $table->string('short_name', 100)->nullable()->comment('Abbreviated name');
            $table->string('slug', 191)->unique()->comment('URL-friendly identifier');
            $table->string('brand', 100)->nullable()->comment('Brand name');
            $table->string('model', 100)->nullable()->comment('Model number/name');
            $table->string('category', 100)->nullable()->comment('Machine category');

            // Manufacturer/Vendor Information
            $table->string('vendor', 191)->nullable()->comment('Vendor/supplier name');
            $table->string('manufacturers', 191)->nullable()->comment('Manufacturer name');

            // Media
            $table->json('images')->nullable()->comment('Array of image paths');
            $table->json('video_clips')->nullable()->comment('Array of video URLs or paths');
            $table->string('primary_image', 191)->nullable()->comment('Main display image');

            // Specifications
            $table->text('description')->nullable()->comment('Full description');
            $table->json('specifications')->nullable()->comment('Technical specifications');
            $table->json('functionalities')->nullable()->comment('Array of features/capabilities');
            $table->json('website_links')->nullable()->comment('Related external links');

            // Contact Information
            $table->json('contact_info')->nullable()->comment('Contact details (phone, email, etc.)');

            // Pricing
            $table->decimal('price_ssp', 15, 2)->nullable()->comment('Price in South Sudan Pounds');
            $table->decimal('price_usd', 15, 2)->nullable()->comment('Price in US Dollars');
            $table->boolean('price_publishable')->default(true)->comment('Show price publicly');
            $table->string('price_note', 191)->nullable()->comment('Additional price information');

            // Status Management
            $table->boolean('is_published')->default(false)->comment('Public visibility');
            $table->enum('listing_status', [
                'draft',
                'pending_payment',
                'pending_approval',
                'published',
                'paused',
                'expired',
                'rejected',
                'suspended'
            ])->default('draft')->comment('Current listing status');

            $table->enum('payment_status', [
                'pending',
                'paid',
                'overdue',
                'exempt'
            ])->default('pending')->comment('Payment status');

            // Payment Tracking
            $table->timestamp('next_payment_date')->nullable()->comment('Next payment due date');
            $table->timestamp('published_at')->nullable()->comment('When listing was published');
            $table->timestamp('expires_at')->nullable()->comment('Listing expiration date');
            $table->timestamp('approved_at')->nullable()->comment('When admin approved');

            // Admin Review
            $table->foreignId('approved_by')->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('Admin who approved listing');
            $table->text('rejection_reason')->nullable()->comment('Reason for rejection');

            // Statistics
            $table->integer('view_count')->default(0)->comment('Number of views');
            $table->integer('inquiry_count')->default(0)->comment('Number of inquiries');

            // Timestamps and Soft Deletes
            $table->timestamps();
            $table->softDeletes();

            // Indexes for performance
            $table->index('user_id');
            $table->index('brand');
            $table->index('category');
            $table->index('listing_status');
            $table->index('payment_status');
            $table->index('is_published');
            $table->index('price_publishable');
            $table->index('created_at');
            $table->index(['category', 'is_published']);
            $table->index(['brand', 'is_published']);
        });
    }

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