<?php

declare(strict_types=1);

namespace Database\Seeders;

use PDO;

final class ReferenceDataSeeder implements SeederInterface
{
    public function run(PDO $pdo): void
    {
        $businessLineIds = $pdo->query('SELECT slug, id FROM business_lines')->fetchAll(PDO::FETCH_KEY_PAIR);

        $this->seedServiceTypes($pdo, $businessLineIds);
        $this->seedServicePlans($pdo, $businessLineIds);
    }

    private function seedServiceTypes(PDO $pdo, array $businessLineIds): void
    {
        $types = [
            ['slug' => 'homecare', 'name' => 'Weekly Homecare Visit', 'minutes' => 180, 'price' => 120.00],
            ['slug' => 'homecare', 'name' => 'Post-Surgery Assessment', 'minutes' => 60, 'price' => 90.00],
            ['slug' => 'janitorial', 'name' => 'Nightly Commercial Clean', 'minutes' => 120, 'price' => 220.00],
            ['slug' => 'janitorial', 'name' => 'Deep Clean / Walkthrough', 'minutes' => 240, 'price' => 480.00],
        ];

        $stmt = $pdo->prepare(
            'INSERT INTO service_types (business_line_id, name, default_duration_minutes, default_price, currency, is_active)
             VALUES (:business_line_id, :name, :minutes, :price, "CAD", 1)'
        );

        $count = 0;
        foreach ($types as $type) {
            if (!isset($businessLineIds[$type['slug']])) {
                continue;
            }

            $exists = $pdo->prepare('SELECT COUNT(*) FROM service_types WHERE business_line_id = :bl AND name = :name');
            $exists->execute(['bl' => $businessLineIds[$type['slug']], 'name' => $type['name']]);
            if ((int) $exists->fetchColumn() > 0) {
                continue;
            }

            $stmt->execute([
                'business_line_id' => $businessLineIds[$type['slug']],
                'name' => $type['name'],
                'minutes' => $type['minutes'],
                'price' => $type['price'],
            ]);
            $count++;
        }

        echo "  seeded {$count} service types\n";
    }

    private function seedServicePlans(PDO $pdo, array $businessLineIds): void
    {
        $plans = [
            ['slug' => 'homecare', 'name' => 'Weekly Care Plus', 'frequency' => 'weekly', 'price' => 480.00],
            ['slug' => 'homecare', 'name' => 'Recovery Care', 'frequency' => 'monthly', 'price' => 1050.00],
            ['slug' => 'janitorial', 'name' => 'Nightly Commercial', 'frequency' => 'monthly', 'price' => 4400.00],
            ['slug' => 'janitorial', 'name' => 'Basic Visit', 'frequency' => 'one_time', 'price' => 220.00],
        ];

        $stmt = $pdo->prepare(
            'INSERT INTO service_plans (business_line_id, name, billing_frequency, base_price, currency, is_active, created_at, updated_at)
             VALUES (:business_line_id, :name, :frequency, :price, "CAD", 1, NOW(), NOW())'
        );

        $count = 0;
        foreach ($plans as $plan) {
            if (!isset($businessLineIds[$plan['slug']])) {
                continue;
            }

            $exists = $pdo->prepare('SELECT COUNT(*) FROM service_plans WHERE business_line_id = :bl AND name = :name');
            $exists->execute(['bl' => $businessLineIds[$plan['slug']], 'name' => $plan['name']]);
            if ((int) $exists->fetchColumn() > 0) {
                continue;
            }

            $stmt->execute([
                'business_line_id' => $businessLineIds[$plan['slug']],
                'name' => $plan['name'],
                'frequency' => $plan['frequency'],
                'price' => $plan['price'],
            ]);
            $count++;
        }

        echo "  seeded {$count} service plans\n";
    }
}
