<?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
    {
        // Vendor applications (verification pipeline)
        Schema::create('vendor_applications', function (Blueprint $table) {
            $table->id();
            $table->string('business_name');
            $table->string('owner_name');
            $table->string('phone', 20)->unique();
            $table->string('email')->nullable();
            $table->string('cnic', 15)->unique()->nullable();
            $table->text('address')->nullable();
            $table->string('locality')->nullable();
            $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
            $table->string('business_type')->nullable(); // shop, home-based, pharmacy, wholesale

            // Verification steps (8-step pipeline)
            $table->boolean('step1_phone_verified')->default(false);  // OTP
            $table->boolean('step2_cnic_uploaded')->default(false);
            $table->boolean('step3_business_info_complete')->default(false);
            $table->boolean('step4_business_proof_uploaded')->default(false);
            $table->boolean('step5_selfie_uploaded')->default(false);
            $table->boolean('step6_bank_details_complete')->default(false);
            $table->integer('current_step')->default(1);

            // Document paths (stored in private storage)
            $table->string('cnic_front_path')->nullable();
            $table->string('cnic_back_path')->nullable();
            $table->string('business_proof_path')->nullable();
            $table->string('selfie_with_cnic_path')->nullable();

            // Bank/wallet details
            $table->enum('settlement_method', ['bank', 'easypaisa', 'jazzcash'])->nullable();
            $table->string('settlement_account_name')->nullable();
            $table->string('settlement_account_number')->nullable();

            $table->enum('status', ['draft', 'submitted', 'under_review', 'approved', 'rejected'])->default('draft');
            $table->text('rejection_reason')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamps();
        });

        // Approved vendor profiles
        Schema::create('vendors', function (Blueprint $table) {
            $table->id();
            $table->foreignId('application_id')->constrained('vendor_applications');
            $table->string('store_name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->string('logo')->nullable();
            $table->string('banner')->nullable();
            $table->string('phone', 20);
            $table->string('email')->nullable();
            $table->text('address');
            $table->string('locality');
            $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();

            // Auth
            $table->string('password');
            $table->string('pin', 6)->nullable();
            $table->rememberToken();

            // Tier system
            $table->enum('tier', ['new', 'verified', 'premium'])->default('new');
            $table->decimal('commission_rate', 5, 2)->default(12.00);
            $table->integer('total_orders')->default(0);
            $table->integer('complaint_count')->default(0);
            $table->decimal('fulfilment_rate', 5, 2)->default(100.00);
            $table->decimal('rating_average', 3, 2)->default(0.00);
            $table->integer('rating_count')->default(0);

            // Trial period (30 orders)
            $table->boolean('trial_period_complete')->default(false);

            // Delivery options
            $table->boolean('uses_platform_delivery')->default(true);
            $table->boolean('uses_self_delivery')->default(false);
            $table->decimal('min_order_value', 10, 2)->default(0);
            $table->json('delivery_zones')->nullable(); // zone names/areas served

            // Settlement
            $table->enum('settlement_method', ['bank', 'easypaisa', 'jazzcash'])->default('bank');
            $table->string('settlement_account_name')->nullable();
            $table->string('settlement_account_number')->nullable();
            $table->decimal('pending_settlement', 12, 2)->default(0);

            $table->enum('status', ['active', 'suspended', 'blacklisted'])->default('active');
            $table->timestamp('suspended_at')->nullable();
            $table->text('suspension_reason')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('vendor_documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->constrained()->cascadeOnDelete();
            $table->string('type'); // cnic_front, cnic_back, business_proof, selfie_cnic
            $table->string('path');
            $table->enum('status', ['pending', 'verified', 'rejected'])->default('pending');
            $table->string('rejection_reason')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('vendor_documents');
        Schema::dropIfExists('vendors');
        Schema::dropIfExists('vendor_applications');
    }
};
