<?php

namespace Database\Seeders;

use App\Models\MembershipCategory;
use App\Models\SubscriptionFee;
use Illuminate\Database\Seeder;

/**
 * SubscriptionFeeSeeder
 *
 * Seeds one annual subscription fee per SSHIA membership category.
 * Subscription fees are denominated in US Dollars (USD) to reflect
 * SSHIA's international membership base and to provide a stable
 * pricing reference in South Sudan's dynamic economic environment.
 *
 * Fee amounts are structured to reflect the nature and benefits of
 * each membership category, with reduced rates for graduate members
 * and waived fees for honorary and founder members.
 *
 * Depends on: MembershipCategorySeeder
 * Uses firstOrCreate keyed on (membership_category_id, name) for idempotency.
 */
class SubscriptionFeeSeeder extends Seeder
{
    public function run(): void
    {
        // Map category slug => fee configuration
        $feeConfig = [
            'founder-members' => [
                'name'         => 'Founder Member Annual Subscription',
                'description'  => 'Annual subscription fee for Founder Members of SSHIA. Founder Members enjoy a lifetime honorary subscription in recognition of their foundational contribution to establishing the Association.',
                'amount'       => 0.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'ordinary-members' => [
                'name'         => 'Ordinary Member Annual Subscription',
                'description'  => 'Annual subscription fee for Ordinary Members of SSHIA. This fee supports the operational activities of the Association and grants full access to all SSHIA programmes, resources, and governance processes.',
                'amount'       => 50.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'associate-members' => [
                'name'         => 'Associate Member Annual Subscription',
                'description'  => 'Annual subscription fee for Associate Members of SSHIA. Associate Members are non-South Sudanese health and informatics professionals who contribute to SSHIA\'s mission of advancing health informatics in South Sudan.',
                'amount'       => 75.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'honorary-members' => [
                'name'         => 'Honorary Member Annual Subscription',
                'description'  => 'Annual subscription fee for Honorary Members of SSHIA. Honorary Members are nominated on merit and their subscription is waived as a mark of recognition for their exceptional contribution to health informatics.',
                'amount'       => 0.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'institutional-members' => [
                'name'         => 'Institutional Member Annual Subscription',
                'description'  => 'Annual subscription fee for Institutional Members of SSHIA, including corporate organisations, academic institutions, and research centres. This fee reflects the institutional benefits and co-branding opportunities available to institutional members.',
                'amount'       => 200.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'graduate-members' => [
                'name'         => 'Graduate Member Annual Subscription',
                'description'  => 'Reduced annual subscription fee for Graduate Members of SSHIA. This concessionary rate supports emerging health informatics professionals during their transition from academic study to full professional practice.',
                'amount'       => 20.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
            'affiliate-members' => [
                'name'         => 'Affiliate Member Annual Subscription',
                'description'  => 'Annual subscription fee for Affiliate Members of SSHIA. Affiliate Members are international organisations that share an interest in health informatics and collaborate with SSHIA on advocacy, research, and knowledge-sharing initiatives.',
                'amount'       => 150.00,
                'currency'     => 'USD',
                'billing_cycle'=> 'annual',
                'is_default'   => true,
                'is_active'    => true,
            ],
        ];

        $seeded = 0;

        foreach ($feeConfig as $categorySlug => $feeData) {
            $category = MembershipCategory::where('slug', $categorySlug)->first();

            if (! $category) {
                $this->command->warn("SubscriptionFeeSeeder: Category '{$categorySlug}' not found. Skipping.");
                continue;
            }

            // Check if a default fee already exists for this category
            $exists = SubscriptionFee::where('membership_category_id', $category->id)
                ->where('name', $feeData['name'])
                ->exists();

            if ($exists) {
                $this->command->info("SubscriptionFeeSeeder: Fee for '{$categorySlug}' already exists. Skipping.");
                continue;
            }

            SubscriptionFee::create(array_merge($feeData, [
                'membership_category_id' => $category->id,
            ]));

            $seeded++;
        }

        $this->command->info("SubscriptionFeeSeeder: {$seeded} subscription fees seeded successfully.");
    }
}
