<?php

namespace Database\Seeders;

use App\Models\PaymentConfiguration;
use Illuminate\Database\Seeder;

/**
 * PaymentConfigurationSeeder
 *
 * Seeds the default payment configuration record for the SSHIA platform.
 * This record establishes the initial payment settings for the Health
 * Directory listing service (institution and machine listings), with
 * payments disabled by default pending configuration of MTN MoMo
 * API credentials by the Super Manager.
 *
 * The seeder creates two default records — one for institution listings
 * and one for digital machine listings — both set to free (is_free = true)
 * until the Super Manager configures pricing through the admin interface.
 *
 * Uses firstOrCreate keyed on service_type for idempotency — safe to re-run.
 */
class PaymentConfigurationSeeder extends Seeder
{
    public function run(): void
    {
        $configurations = [
            [
                'service_type'      => 'institution',
                'is_free'           => true,
                'amount_ssp'        => 0.00,
                'amount_usd'        => null,
                'frequencies'       => ['monthly', 'quarterly', 'yearly'],
                'default_frequency' => 'yearly',
                'grace_period_days' => 30,
                'description'       => 'Listing fee configuration for health institution registrations in the SSHIA Health Directory. Set is_free to false and configure amount_ssp to enable paid listings.',
                'environment'       => 'sandbox',
                'is_active'         => true,
            ],
            [
                'service_type'      => 'machine',
                'is_free'           => true,
                'amount_ssp'        => 0.00,
                'amount_usd'        => null,
                'frequencies'       => ['monthly', 'quarterly', 'yearly'],
                'default_frequency' => 'yearly',
                'grace_period_days' => 30,
                'description'       => 'Listing fee configuration for digital health machine registrations in the SSHIA Health Directory. Set is_free to false and configure amount_ssp to enable paid listings.',
                'environment'       => 'sandbox',
                'is_active'         => true,
            ],
        ];

        $seeded = 0;

        foreach ($configurations as $config) {
            $exists = PaymentConfiguration::where('service_type', $config['service_type'])->exists();

            if ($exists) {
                $this->command->info("PaymentConfigurationSeeder: Config for service_type '{$config['service_type']}' already exists. Skipping.");
                continue;
            }

            PaymentConfiguration::create($config);
            $seeded++;
        }

        $this->command->info("PaymentConfigurationSeeder: {$seeded} payment configuration records seeded successfully.");
    }
}
