<?php

namespace Database\Seeders;

use App\Models\BillingAddress;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

/**
 * DemoSeeder
 *
 * Creates 2 demo customer accounts that visitors can sign into to explore the platform.
 * Orchestrates all demo-only sub-seeders in the correct order.
 *
 * Demo Accounts:
 *   customer@demo.com  / demo1234  (Standard customer — orders, tickets, reviews)
 *   pro@demo.com       / demo1234  (Pro customer — subscriptions, licenses, tickets)
 */
class DemoSeeder extends Seeder
{
    public function run(): void
    {
        $this->createDemoUsers();

        // Ensures product gallery images from storage/demo/ are attached on every refresh.
        // Product creation methods are idempotent (skip if already exist).
        $this->call(ProductTableSeeder::class);

        $this->call(OrderSeeder::class);
        $this->call(SubscriptionSeeder::class);
        $this->call(TicketSeeder::class);
        $this->call(FaqSeeder::class);
        $this->call(ReviewSeeder::class);
        $this->call(BlogCategoryTableSeeder::class);
        $this->call(BlogPostTableSeeder::class);
        $this->call(PostCategoryTableSeeder::class);
        $this->call(PostCommentTableSeeder::class);
        $this->call(CouponsTableSeeder::class);
    }

    private function createDemoUsers(): void
    {
        $customers = [
            [
                'first_name'   => 'Alex',
                'last_name'    => 'Demo',
                'email'        => 'customer@demo.com',
                'password'     => 'demo1234',
                'country_id'   => 231, // United States
                'state_id'     => 3956, // New York
                'city'         => 'New York',
                'address'      => '124 Broadway, Suite 200',
                'zip'          => '10001',
                'phone_number' => '+12125550101',
                'company_name' => 'Demo Ventures LLC',
            ],
            [
                'first_name'   => 'Sarah',
                'last_name'    => 'Pro',
                'email'        => 'pro@demo.com',
                'password'     => 'demo1234',
                'country_id'   => 230, // United Kingdom
                'state_id'     => 3805, // Aberdeen
                'city'         => 'London',
                'address'      => '45 Baker Street',
                'zip'          => 'W1U 8EW',
                'phone_number' => '+442079460958',
                'company_name' => 'Pro Digital Ltd',
            ],
        ];

        foreach ($customers as $customer) {
            // Skip if user already exists (for idempotency)
            if (User::where('email', $customer['email'])->exists()) {
                continue;
            }

            $user = User::create([
                'first_name'        => $customer['first_name'],
                'last_name'         => $customer['last_name'],
                'email'             => $customer['email'],
                'password'          => Hash::make($customer['password']),
                'email_verified_at' => Carbon::now()->subDays(rand(1, 30)),
                'last_login'        => Carbon::now()->subDays(rand(0, 7)),
            ]);

            $user->roles()->sync([2]); // customer role

            BillingAddress::create([
                'user_id'      => $user->id,
                'first_name'   => $customer['first_name'],
                'last_name'    => $customer['last_name'],
                'company_name' => $customer['company_name'],
                'address'      => $customer['address'],
                'city'         => $customer['city'],
                'state_id'     => $customer['state_id'],
                'zip'          => $customer['zip'],
                'country_id'   => $customer['country_id'],
                'phone_number' => $customer['phone_number'],
            ]);
        }
    }
}
