<?php

namespace Database\Seeders;

use App\Models\AppSetting;
use App\Models\Frame;
use App\Models\FrameCategory;
use App\Models\FrameSubCategory;
use App\Models\Lens;
use App\Models\LensCategory;
use App\Models\LensSubCategory;
use App\Models\Package;
use App\Models\Subscription;
use App\Models\User;
use App\Models\VendorProfile;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 1. App Settings
        AppSetting::updateOrCreate(
            ['id' => 1],
            [
                'app_version_android' => '1.0.0',
                'app_version_ios' => '1.0.0',
                'force_update' => false,
                'razorpay_key_id' => 'rzp_test_mLEsClgb3VPQfg',
                'razorpay_key_secret' => 'sample_secret_key',
                'contact_phone' => '+91 9876543210',
                'contact_email' => 'support@myopticals.com',
                'privacy_policy_url' => 'https://myopticals.com/privacy-policy',
                'terms_conditions_url' => 'https://myopticals.com/terms-and-conditions',
            ]
        );

        // 2. SuperAdmin User
        User::updateOrCreate(
            ['email' => 'admin@myopticals.com'],
            [
                'name' => 'Super Administrator',
                'phone' => '9999999999',
                'role' => 'superadmin',
                'unique_token' => 'ADMIN001',
                'password' => Hash::make('Admin@123'),
                'status' => true,
                'is_first_login' => false,
            ]
        );

        // 3. Subscription Packages
        $pkg1 = Package::updateOrCreate(
            ['name' => 'Starter Monthly'],
            [
                'months' => 1,
                'price' => 499.00,
                'payable_charge' => 499.00,
                'description' => 'Ideal for single optical shops starting with AR try-on catalog.',
                'is_active' => true,
            ]
        );

        $pkg2 = Package::updateOrCreate(
            ['name' => 'Pro Half-Yearly'],
            [
                'months' => 6,
                'price' => 2499.00,
                'payable_charge' => 2499.00,
                'description' => 'Best value for growing optical stores with unlimited product uploads.',
                'is_active' => true,
            ]
        );

        $pkg3 = Package::updateOrCreate(
            ['name' => 'Enterprise Annual'],
            [
                'months' => 12,
                'price' => 4499.00,
                'payable_charge' => 4499.00,
                'description' => 'Complete solution for premium optical chains with priority AR placement.',
                'is_active' => true,
            ]
        );

        // 4. Sample Vendor 1: "VisionCraft Opticals" (Ahmedabad)
        $vendor1 = User::updateOrCreate(
            ['email' => 'vendor@visioncraft.com'],
            [
                'name' => 'Rajesh Sharma',
                'phone' => '9876543210',
                'role' => 'vendor',
                'unique_token' => 'Vendor101',
                'password' => Hash::make('Vendor@123'),
                'status' => true,
                'is_first_login' => false,
            ]
        );

        VendorProfile::updateOrCreate(
            ['user_id' => $vendor1->id],
            [
                'vendor_name' => 'VisionCraft Opticals & Eyewear',
                'contact_person_name' => 'Rajesh Sharma',
                'address' => 'Shop 12, Crystal Plaza, MG Road',
                'city' => 'Ahmedabad',
                'latitude' => 23.0225,
                'longitude' => 72.5714,
                'whatsapp_number' => '+919876543210',
                'is_active' => true,
                'is_renewal' => false,
            ]
        );

        Subscription::updateOrCreate(
            ['user_id' => $vendor1->id],
            [
                'package_id' => $pkg2->id,
                'start_date' => now(),
                'expires_at' => now()->addMonths(6),
                'status' => 'active',
            ]
        );

        // Vendor 1 Categories & Frames
        $fc1 = FrameCategory::updateOrCreate(['vendor_id' => $vendor1->id, 'name' => 'Eyeglasses'], ['is_active' => true]);
        $fc2 = FrameCategory::updateOrCreate(['vendor_id' => $vendor1->id, 'name' => 'Sunglasses'], ['is_active' => true]);

        $fsc1 = FrameSubCategory::updateOrCreate(['frame_category_id' => $fc1->id, 'name' => 'Aviator'], ['vendor_id' => $vendor1->id, 'is_active' => true]);
        $fsc2 = FrameSubCategory::updateOrCreate(['frame_category_id' => $fc1->id, 'name' => 'Rectangle'], ['vendor_id' => $vendor1->id, 'is_active' => true]);
        $fsc3 = FrameSubCategory::updateOrCreate(['frame_category_id' => $fc2->id, 'name' => 'Round Vintage'], ['vendor_id' => $vendor1->id, 'is_active' => true]);

        Frame::updateOrCreate(
            ['vendor_id' => $vendor1->id, 'name' => 'AeroClassic Titanium Aviator'],
            [
                'category_id' => $fc1->id,
                'sub_category_id' => $fsc1->id,
                'description' => 'Ultra-lightweight titanium frame with flexible spring hinges and anti-glare coating.',
                'price' => 1499.00,
                'stock_quantity' => 25,
                'low_stock_threshold' => 5,
                'size' => 'Medium',
                'color' => 'Matte Black',
                'shape' => 'Aviator',
                'rim_type' => 'Full-rim',
                'gender' => 'Unisex',
                'images' => [
                    'https://images.unsplash.com/photo-1511499767150-a48a237f0083?w=800&auto=format&fit=crop&q=80',
                ],
                'tryon_image_url' => 'https://raw.githubusercontent.com/minfosoftware/assets/main/frames/frame_aviator_black.png',
                'is_active' => true,
            ]
        );

        Frame::updateOrCreate(
            ['vendor_id' => $vendor1->id, 'name' => 'UrbanEdge Acetate Rectangle'],
            [
                'category_id' => $fc1->id,
                'sub_category_id' => $fsc2->id,
                'description' => 'Handcrafted Italian acetate eyeglasses with comfortable keyhole bridge.',
                'price' => 1899.00,
                'stock_quantity' => 18,
                'low_stock_threshold' => 5,
                'size' => 'Large',
                'color' => 'Tortoise Brown',
                'shape' => 'Rectangle',
                'rim_type' => 'Full-rim',
                'gender' => 'Men',
                'images' => [
                    'https://images.unsplash.com/photo-1591076482161-42ce6da69f67?w=800&auto=format&fit=crop&q=80',
                ],
                'tryon_image_url' => 'https://raw.githubusercontent.com/minfosoftware/assets/main/frames/frame_rectangle_tortoise.png',
                'is_active' => true,
            ]
        );

        // 5. Sample Vendor 2: "LuxeOptics Mumbai" (Mumbai)
        $vendor2 = User::updateOrCreate(
            ['email' => 'contact@luxeoptics.in'],
            [
                'name' => 'Amitabh Sen',
                'phone' => '9820098200',
                'role' => 'vendor',
                'unique_token' => 'Vendor102',
                'password' => Hash::make('Vendor@123'),
                'status' => true,
                'is_first_login' => false,
            ]
        );

        VendorProfile::updateOrCreate(
            ['user_id' => $vendor2->id],
            [
                'vendor_name' => 'LuxeOptics Boutique Mumbai',
                'contact_person_name' => 'Amitabh Sen',
                'address' => 'Ground Floor, Bandra West',
                'city' => 'Mumbai',
                'latitude' => 19.0596,
                'longitude' => 72.8295,
                'whatsapp_number' => '+919820098200',
                'is_active' => true,
                'is_renewal' => false,
            ]
        );

        Subscription::updateOrCreate(
            ['user_id' => $vendor2->id],
            [
                'package_id' => $pkg3->id,
                'start_date' => now(),
                'expires_at' => now()->addMonths(12),
                'status' => 'active',
            ]
        );

        Frame::updateOrCreate(
            ['vendor_id' => $vendor2->id, 'name' => 'Parisienne Cat-Eye Gold'],
            [
                'category_id' => $fc2->id,
                'sub_category_id' => $fsc3->id,
                'description' => 'Glamorous cat-eye silhouette in rose gold metal with anti-scratch polarized lenses.',
                'price' => 2499.00,
                'stock_quantity' => 12,
                'low_stock_threshold' => 4,
                'size' => 'Medium',
                'color' => 'Rose Gold',
                'shape' => 'Cat-Eye',
                'rim_type' => 'Full-rim',
                'gender' => 'Women',
                'images' => [
                    'https://images.unsplash.com/photo-1508296695146-257a814070b4?w=800&auto=format&fit=crop&q=80',
                ],
                'is_active' => true,
            ]
        );

        // Contact Lenses
        $lc1 = LensCategory::updateOrCreate(['vendor_id' => $vendor1->id, 'name' => 'Color Contact Lenses'], ['is_active' => true]);
        $lsc1 = LensSubCategory::updateOrCreate(['lens_category_id' => $lc1->id, 'name' => 'Monthly Disposable'], ['vendor_id' => $vendor1->id, 'is_active' => true]);

        Lens::updateOrCreate(
            ['vendor_id' => $vendor1->id, 'name' => 'AquaGlow Ocean Blue Monthly Lenses'],
            [
                'category_id' => $lc1->id,
                'sub_category_id' => $lsc1->id,
                'description' => 'Breathable hydrogel lenses providing vibrant natural blue eye color with UV protection.',
                'price' => 750.00,
                'stock_quantity' => 40,
                'low_stock_threshold' => 10,
                'size' => 'Standard (14.2mm)',
                'color' => 'Ocean Blue',
                'lens_type' => 'Monthly',
                'images' => [
                    'https://images.unsplash.com/photo-1583394838336-acd977736f90?w=800&auto=format&fit=crop&q=80',
                ],
                'is_active' => true,
            ]
        );

        Lens::updateOrCreate(
            ['vendor_id' => $vendor1->id, 'name' => 'HazelGlow Mystic Brown Daily Lenses'],
            [
                'category_id' => $lc1->id,
                'sub_category_id' => $lsc1->id,
                'description' => 'Natural hazel tone daily disposable lenses with high moisture retention.',
                'price' => 999.00,
                'stock_quantity' => 30,
                'low_stock_threshold' => 8,
                'size' => 'Standard (14.2mm)',
                'color' => 'Hazel Brown',
                'lens_type' => 'Daily',
                'images' => [
                    'https://images.unsplash.com/photo-1583394838336-acd977736f90?w=800&auto=format&fit=crop&q=80',
                ],
                'is_active' => true,
            ]
        );

        // 6. Sample Customer
        User::updateOrCreate(
            ['email' => 'priya@gmail.com'],
            [
                'name' => 'Priya Patel',
                'phone' => '9123456780',
                'role' => 'customer',
                'unique_token' => 'Customer101',
                'password' => Hash::make('Customer@123'),
                'status' => true,
                'is_first_login' => false,
            ]
        );
    }
}
