<?php

namespace Database\Seeders;

use App\Models\Area;
use App\Models\PartnerProfile;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedSettings();
        $this->seedUsers();
        $this->seedAreas();
        $this->seedServices();
        $this->seedPartners();
    }

    private function seedSettings(): void
    {
        $defaults = [
            'site_name' => ['Befikreyy', 'text', 'general'],
            'site_tagline' => ['Handyman & Home Services Marketplace', 'text', 'general'],
            'site_phone' => ['+91 7291061623', 'text', 'general'],
            'site_email' => ['contactus@befikreyyhomes.com', 'text', 'general'],
            'site_address' => ['Flat F701, Supertech Ecociti, Sector 137, Noida', 'text', 'general'],
            'site_whatsapp' => ['917291061623', 'text', 'general'],
            'tax_percent' => ['5', 'text', 'billing'],
            'currency' => ['₹', 'text', 'general'],
        ];
        foreach ($defaults as $k => [$v, $type, $group]) {
            Setting::set($k, $v, $type, $group);
        }
    }

    private function seedUsers(): void
    {
        User::updateOrCreate(
            ['email' => 'admin@befikreyy.com'],
            [
                'name' => 'Befikreyy Admin',
                'password' => Hash::make('admin123'),
                'role' => 'admin',
                'is_partner_approved' => true,
                'phone' => '7291061623',
            ]
        );

        User::updateOrCreate(
            ['email' => 'customer@demo.com'],
            [
                'name' => 'Demo Customer',
                'password' => Hash::make('customer123'),
                'role' => 'customer',
                'phone' => '9876500001',
                'address' => 'Tower B, Supertech Ecociti, Sector 137, Noida',
            ]
        );
    }

    private function seedAreas(): void
    {
        $areas = [
            ['Sector 137, Noida', 'sector-137', '201305', 28.5018, 77.4055],
            ['Sector 135, Noida', 'sector-135', '201301', 28.4970, 77.3995],
            ['Sector 142, Noida', 'sector-142', '201306', 28.4960, 77.4120],
            ['Sector 144, Noida', 'sector-144', '201306', 28.4850, 77.4150],
            ['Sector 168, Noida', 'sector-168', '201306', 28.4670, 77.4380],
            ['Sector 50, Noida', 'sector-50', '201301', 28.5720, 77.3690],
            ['Sector 62, Noida', 'sector-62', '201301', 28.6200, 77.3640],
            ['Sector 18, Noida', 'sector-18', '201301', 28.5700, 77.3260],
            ['Greater Noida West', 'greater-noida-west', '201306', 28.4800, 77.4300],
            ['Indirapuram, Ghaziabad', 'indirapuram', '201014', 28.6410, 77.3700],
        ];

        foreach ($areas as [$name, $slug, $pin, $lat, $lng]) {
            Area::updateOrCreate(['slug' => $slug], [
                'name' => $name,
                'city' => str_contains($name, 'Ghaziabad') ? 'Ghaziabad' : 'Noida',
                'pincode' => $pin,
                'lat' => $lat,
                'lng' => $lng,
                'is_active' => true,
            ]);
        }
    }

    private function seedServices(): void
    {
        $categories = [
            ['Car Wash', 'car-care', '🚗', 1],
            ['Property Care', 'property-care', '🏠', 2],
        ];

        $categoryIds = [];
        foreach ($categories as [$name, $slug, $icon, $sort]) {
            $cat = ServiceCategory::updateOrCreate(['slug' => $slug], [
                'name' => $name,
                'icon' => $icon,
                'sort_order' => $sort,
                'is_active' => true,
            ]);
            $categoryIds[$slug] = $cat->id;
        }

        $services = [
            // Car Wash category — Sedan, Hatchback, SUV, Luxury (matches the bolt.host site)
            ['car-care', 'Car Wash (Sedan)', 'car-wash-sedan', 'Sedan · monthly doorstep car wash', 'Premium doorstep car wash monthly for sedan cars.', 150000, 200000, 60, true, ['Exterior foam wash', 'Tyre dressing', 'Interior vacuum', 'Dashboard polish'], 1],
            ['car-care', 'Car Wash (Hatchback)', 'car-wash-hatchback', 'Hatchback · monthly doorstep car wash', 'Premium doorstep car wash monthly for hatchback cars.', 100000, 150000, 45, false, ['Exterior foam wash', 'Tyre dressing', 'Interior vacuum', 'Dashboard polish'], 2],
            ['car-care', 'Car Wash (SUV)', 'car-wash-suv', 'SUV · most popular monthly doorstep wash', 'Car wash monthly for SUV — our most popular service.', 200000, 250000, 60, true, ['Exterior foam wash', 'Tyre dressing', 'Interior vacuum', 'Dashboard polish'], 3],
            ['car-care', 'Premium Car Wash (Luxury)', 'premium-car-wash-luxury', 'Luxury · premium hand-wash + wax', 'Premium car wash for luxury vehicles — hand-wash, wax and detailing.', 300000, 400000, 90, true, ['pH neutral shampoo', 'Hand wax protection', 'Premium interior detailing', 'Tyre & chrome shine'], 4],

            // Property Care category — 1BHK to 5BHK
            ['property-care', 'Property Care (1 BHK)', 'property-care-1bhk', '1 BHK · monthly property care', 'Professional property maintenance and caretaker service for 1 BHK vacant and tenanted flats.', 150000, 200000, 60, false, ['Monthly visit', 'Photo report', 'WhatsApp update', 'Maintenance flags'], 1],
            ['property-care', 'Property Care (2 BHK)', 'property-care-2bhk', '2 BHK · most popular monthly property care', 'Property maintenance and caretaker service for 2 BHK flats.', 200000, 250000, 60, true, ['Monthly visit', 'Photo report', 'WhatsApp update', 'Maintenance flags'], 2],
            ['property-care', 'Property Care (3 BHK)', 'property-care-3bhk', '3 BHK · monthly property care', 'Property maintenance and caretaker service for 3 BHK flats.', 250000, 300000, 75, false, ['Monthly visit', 'Photo report', 'WhatsApp update', 'Maintenance flags'], 3],
            ['property-care', 'Property Care (4 BHK)', 'property-care-4bhk', '4 BHK · monthly property care', 'Property maintenance and caretaker service for 4 BHK flats.', 300000, 350000, 90, false, ['Monthly visit', 'Photo report', 'WhatsApp update', 'Maintenance flags'], 4],
            ['property-care', 'Property Care (5 BHK)', 'property-care-5bhk', '5 BHK · monthly property care', 'Property maintenance and caretaker service for 5 BHK flats.', 350000, 400000, 120, false, ['Monthly visit', 'Photo report', 'WhatsApp update', 'Maintenance flags'], 5],
        ];

        foreach ($services as $s) {
            [$catSlug, $name, $slug, $tagline, $desc, $price, $mrp, $duration, $featured, $whatsIn, $sort] = $s;
            Service::updateOrCreate(['slug' => $slug], [
                'category_id' => $categoryIds[$catSlug] ?? null,
                'name' => $name,
                'tagline' => $tagline,
                'description' => $desc,
                'price_paise' => $price,
                'original_price_paise' => $mrp,
                'duration_minutes' => $duration,
                'is_featured' => $featured,
                'is_active' => true,
                'sort_order' => $sort,
                'whats_included' => $whatsIn,
            ]);
        }
    }

    private function seedPartners(): void
    {
        $partners = [
            ['Rajesh Kumar', 'rajesh@partner.befikreyy.com', '9876543210', 'Experienced in car wash & deep cleaning', 8, true, true, 4.8, 142],
            ['Suresh Yadav', 'suresh@partner.befikreyy.com', '9876543211', 'Specialist in AC & appliance repair', 6, true, true, 4.7, 98],
            ['Amit Sharma', 'amit@partner.befikreyy.com', '9876543212', 'Plumbing & electrical expert', 5, true, true, 4.9, 76],
            ['Manoj Singh', 'manoj@partner.befikreyy.com', '9876543213', 'Painter & home renovator', 7, true, true, 4.6, 64],
            ['Vijay Verma', 'vijay@partner.befikreyy.com', '9876543214', 'Pest control & sanitisation', 4, true, true, 4.5, 52],
        ];

        $areas = Area::all();
        $services = Service::all();

        foreach ($partners as [$name, $email, $phone, $bio, $exp, $aadhaar, $police, $rating, $jobs]) {
            $user = User::updateOrCreate(['email' => $email], [
                'name' => $name,
                'password' => Hash::make('partner123'),
                'role' => 'partner',
                'is_partner_approved' => true,
                'phone' => $phone,
            ]);
            $partner = PartnerProfile::updateOrCreate(['user_id' => $user->id], [
                'display_name' => $name,
                'bio' => $bio,
                'experience_years' => $exp,
                'aadhaar_verified' => $aadhaar,
                'police_verified' => $police,
                'is_online' => true,
                'is_available' => true,
                'rating_avg' => $rating,
                'rating_count' => $jobs,
                'jobs_done' => $jobs,
            ]);
            $partner->areas()->syncWithoutDetaching(
                $areas->random(min(rand(3, 5), $areas->count()))->pluck('id')->all()
            );
            $partner->services()->syncWithoutDetaching(
                $services->random(min(rand(3, 6), $services->count()))->pluck('id')->all()
            );
        }
    }
}
