<?php

declare(strict_types=1);

namespace Database\Seeders;

use PDO;

final class WorkspaceSeeder implements SeederInterface
{
    public function run(PDO $pdo): void
    {
        $this->seedSettings($pdo);
        $this->seedIntegrations($pdo);
        $this->seedBillingAccount($pdo);
    }

    private function seedSettings(PDO $pdo): void
    {
        $settings = [
            'workspace_name' => 'A&B Hub',
            'timezone' => 'America/Toronto',
            'date_format' => 'MM/DD/YYYY',
            'default_currency' => 'CAD',
            'support_email' => 'support@abhub.com',
        ];

        $stmt = $pdo->prepare(
            'INSERT INTO settings (setting_key, setting_value, updated_at) VALUES (:key, :value, NOW())
             ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)'
        );

        foreach ($settings as $key => $value) {
            $stmt->execute(['key' => $key, 'value' => $value]);
        }

        echo '  seeded ' . count($settings) . " workspace settings\n";
    }

    private function seedIntegrations(PDO $pdo): void
    {
        $integrations = [
            ['name' => 'Gmail / Google Workspace', 'slug' => 'gmail', 'status' => 'connected'],
            ['name' => 'Twilio Voice & SMS', 'slug' => 'twilio', 'status' => 'connected'],
            ['name' => 'Google Calendar', 'slug' => 'google_calendar', 'status' => 'connected'],
            ['name' => 'QuickBooks Online', 'slug' => 'quickbooks_online', 'status' => 'not_connected'],
            ['name' => 'Slack', 'slug' => 'slack', 'status' => 'not_connected'],
            ['name' => 'Google Business Profile', 'slug' => 'google_business_profile', 'status' => 'not_connected'],
        ];

        $stmt = $pdo->prepare(
            'INSERT INTO integrations (name, slug, status, created_at, updated_at)
             VALUES (:name, :slug, :status, NOW(), NOW())
             ON DUPLICATE KEY UPDATE name = VALUES(name)'
        );

        foreach ($integrations as $integration) {
            $stmt->execute($integration);
        }

        echo '  seeded ' . count($integrations) . " integrations\n";
    }

    private function seedBillingAccount(PDO $pdo): void
    {
        $exists = (int) $pdo->query('SELECT COUNT(*) FROM billing_accounts')->fetchColumn();

        if ($exists > 0) {
            echo "  billing account already present, skipped\n";

            return;
        }

        $stmt = $pdo->prepare(
            'INSERT INTO billing_accounts (plan_name, monthly_amount, currency, billing_cycle, card_last_four, card_brand, renews_on, updated_at)
             VALUES (:plan_name, :monthly_amount, :currency, :billing_cycle, :card_last_four, :card_brand, :renews_on, NOW())'
        );

        $stmt->execute([
            'plan_name' => 'Enterprise',
            'monthly_amount' => 899.00,
            'currency' => 'CAD',
            'billing_cycle' => 'annually',
            'card_last_four' => '4821',
            'card_brand' => 'Visa',
            'renews_on' => date('Y-m-d', strtotime('+6 months')),
        ]);

        echo "  seeded 1 billing account\n";
    }
}
