<?php

namespace Database\Seeders;

use App\Enums\UserRole;
use App\Models\Business;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $demoUsers = [
            ['name' => 'Booklio Super Admin', 'email' => 'admin@booklio.test', 'role' => UserRole::SuperAdmin->value],
            ['name' => 'Sarah Johnson', 'email' => 'owner@booklio.test', 'role' => UserRole::BusinessOwner->value],
            ['name' => 'Emma Wilson', 'email' => 'staff@booklio.test', 'role' => UserRole::Staff->value],
            ['name' => 'Dr. Adam Reed', 'email' => 'adam@primedental.test', 'role' => UserRole::BusinessOwner->value],
            ['name' => 'Mia Carter', 'email' => 'mia@cleanpro.test', 'role' => UserRole::BusinessOwner->value],
        ];

        $users = collect();

        foreach ($demoUsers as $user) {
            $users->put($user['email'], User::query()->updateOrCreate(
                ['email' => $user['email']],
                [
                    'name' => $user['name'],
                    'email_verified_at' => now(),
                    'password' => Hash::make('password'),
                    'role' => $user['role'],
                ]
            ));
        }

        $businesses = [
            [
                'owner' => 'owner@booklio.test',
                'name' => 'Glow Beauty Salon',
                'slug' => 'glow-beauty-salon',
                'email' => 'hello@glowbeauty.test',
                'phone' => '+1 (555) 123-4567',
                'website' => 'https://glowbeauty.example',
                'address' => '24 Bluewater Avenue, Suite 8, Miami, FL',
                'description' => 'Premium salon services, online booking, reminders, and invoices powered by Booklio.',
                'timezone' => 'America/New_York',
                'currency' => 'USD',
                'status' => 'trial',
            ],
            [
                'owner' => 'adam@primedental.test',
                'name' => 'Prime Dental Clinic',
                'slug' => 'prime-dental-clinic',
                'email' => 'admin@primedental.test',
                'phone' => '+1 (555) 450-8821',
                'website' => 'https://primedental.example',
                'address' => '118 Market Street, Austin, TX',
                'description' => 'Dental appointment scheduling, patient reminders, and invoice tracking.',
                'timezone' => 'America/Chicago',
                'currency' => 'USD',
                'status' => 'active',
            ],
            [
                'owner' => 'mia@cleanpro.test',
                'name' => 'CleanPro Services',
                'slug' => 'cleanpro-services',
                'email' => 'hello@cleanpro.test',
                'phone' => '+1 (555) 820-4451',
                'website' => 'https://cleanpro.example',
                'address' => '72 Green Road, Denver, CO',
                'description' => 'Cleaning appointments, staff scheduling, and client CRM demo business.',
                'timezone' => 'America/Denver',
                'currency' => 'USD',
                'status' => 'trial',
            ],
        ];

        foreach ($businesses as $data) {
            $owner = $users->get($data['owner']);

            $business = Business::query()->updateOrCreate(
                ['slug' => $data['slug']],
                [
                    'owner_id' => $owner->id,
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'phone' => $data['phone'],
                    'website' => $data['website'],
                    'address' => $data['address'],
                    'description' => $data['description'],
                    'timezone' => $data['timezone'],
                    'currency' => $data['currency'],
                    'status' => $data['status'],
                    'trial_ends_at' => now()->addDays(14),
                ]
            );

            $business->users()->syncWithoutDetaching([
                $owner->id => [
                    'role' => 'owner',
                    'title' => 'Business Owner',
                    'status' => 'active',
                    'joined_at' => now(),
                ],
            ]);
        }

        $glowBeauty = Business::query()
            ->where('slug', 'glow-beauty-salon')
            ->first();

        $glowBeauty?->users()->syncWithoutDetaching([
                $users->get('staff@booklio.test')->id => [
                    'role' => 'staff',
                    'title' => 'Senior Stylist',
                    'status' => 'active',
                    'joined_at' => now(),
                ],
        ]);

        $this->call(CoreDemoSeeder::class);
    }
}
