<?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 health_institutions table for managing healthcare facility listings.
     * Supports multi-step registration, payment integration, and soft deletes.
     *
     * Requirements: 1.4, 10.1, 10.2
     */
    public function up(): void
    {
        Schema::create('health_institutions', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to users (Health Marketor) - nullable for registration flow
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('Owner/manager of this institution listing');

            // Basic Information
            $table->string('name', 191)->comment('Full institution name');
            $table->string('short_name', 100)->nullable()->comment('Abbreviated name');
            $table->string('slug', 191)->unique()->comment('URL-friendly identifier');
            $table->string('logo', 191)->nullable()->comment('Path to logo image');

            // Location Information
            $table->string('city', 100)->comment('City location');
            $table->string('address', 191)->comment('Street address');
            $table->string('country', 100)->default('South Sudan')->comment('Country');

            // Contact Information
            $table->string('email', 191)->comment('Primary contact email');
            $table->string('phone', 50)->comment('Primary contact phone');
            $table->string('website_url', 191)->nullable()->comment('Institution website');

            // Content
            $table->text('description')->nullable()->comment('Full description');
            $table->json('services')->nullable()->comment('Array of services offered');

            // Gallery
            $table->json('images')->nullable()->comment('Array of image paths');
            $table->json('videos')->nullable()->comment('Array of video URLs or paths');

            // Status Management
            $table->boolean('is_published')->default(false)->comment('Public visibility');
            $table->enum('listing_status', [
                'draft',
                'pending_payment',
                'published',
                'paused',
                'expired',
                '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');

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

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

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

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