<?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. Vendor Profiles
        Schema::create('vendor_profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('vendor_name');
            $table->string('contact_person_name')->nullable();
            $table->text('address')->nullable();
            $table->string('city')->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();
            $table->string('whatsapp_number')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('is_renewal')->default(false);
            $table->timestamps();
        });

        // 2. Subscription Packages
        Schema::create('packages', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('months')->default(1);
            $table->decimal('price', 10, 2)->default(0);
            $table->decimal('payable_charge', 10, 2)->default(0);
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 3. Vendor Subscriptions
        Schema::create('subscriptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('package_id')->constrained()->cascadeOnDelete();
            $table->dateTime('start_date');
            $table->dateTime('expires_at');
            $table->string('status')->default('active'); // active, expired, cancelled
            $table->timestamps();
        });

        // 4. Payment Transactions
        Schema::create('payment_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('package_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('subscription_id')->nullable()->constrained()->nullOnDelete();
            $table->string('txn_id')->unique();
            $table->string('order_id')->nullable();
            $table->string('payment_id')->nullable();
            $table->decimal('amount', 10, 2);
            $table->string('currency')->default('INR');
            $table->string('status')->default('pending'); // pending, success, failed
            $table->string('gateway')->default('razorpay');
            $table->text('gateway_response')->nullable();
            $table->timestamps();
        });

        // 5. Frame Categories & Sub-Categories
        Schema::create('frame_categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->nullable()->constrained('users')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('frame_sub_categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('frame_category_id')->constrained()->cascadeOnDelete();
            $table->foreignId('vendor_id')->nullable()->constrained('users')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 6. Frames (Glasses / Sunglasses)
        Schema::create('frames', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('category_id')->nullable()->constrained('frame_categories')->nullOnDelete();
            $table->foreignId('sub_category_id')->nullable()->constrained('frame_sub_categories')->nullOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price', 10, 2)->default(0);
            $table->string('size')->nullable(); // Small, Medium, Large
            $table->string('color')->nullable();
            $table->string('shape')->nullable(); // Aviator, Round, Rectangle, Wayfarer, Cat-Eye
            $table->string('rim_type')->nullable(); // Full-rim, Half-rim, Rimless
            $table->string('gender')->nullable(); // Men, Women, Unisex, Kids
            $table->json('images')->nullable(); // Array of image URLs
            $table->string('tryon_image_url')->nullable(); // Transparent PNG for AR try-on
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 7. Lens Categories & Sub-Categories
        Schema::create('lens_categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->nullable()->constrained('users')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('lens_sub_categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('lens_category_id')->constrained()->cascadeOnDelete();
            $table->foreignId('vendor_id')->nullable()->constrained('users')->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 8. Lenses (Contact Lenses)
        Schema::create('lenses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('category_id')->nullable()->constrained('lens_categories')->nullOnDelete();
            $table->foreignId('sub_category_id')->nullable()->constrained('lens_sub_categories')->nullOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price', 10, 2)->default(0);
            $table->string('size')->nullable();
            $table->string('color')->nullable();
            $table->string('lens_type')->nullable(); // Daily, Monthly, Yearly, Disposable
            $table->json('images')->nullable();
            $table->string('tryon_image_url')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 9. App Settings
        Schema::create('app_settings', function (Blueprint $table) {
            $table->id();
            $table->string('app_version_android')->default('1.0.0');
            $table->string('app_version_ios')->default('1.0.0');
            $table->boolean('force_update')->default(false);
            $table->string('razorpay_key_id')->nullable();
            $table->string('razorpay_key_secret')->nullable();
            $table->string('contact_phone')->nullable();
            $table->string('contact_email')->nullable();
            $table->string('privacy_policy_url')->nullable();
            $table->string('terms_conditions_url')->nullable();
            $table->timestamps();
        });

        // 10. Customer Wishlist / Favorites
        Schema::create('wishlists', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('item_type'); // frame, lens
            $table->unsignedBigInteger('item_id');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('wishlists');
        Schema::dropIfExists('app_settings');
        Schema::dropIfExists('lenses');
        Schema::dropIfExists('lens_sub_categories');
        Schema::dropIfExists('lens_categories');
        Schema::dropIfExists('frames');
        Schema::dropIfExists('frame_sub_categories');
        Schema::dropIfExists('frame_categories');
        Schema::dropIfExists('payment_transactions');
        Schema::dropIfExists('subscriptions');
        Schema::dropIfExists('packages');
        Schema::dropIfExists('vendor_profiles');
    }
};
