<?php

namespace Database\Seeders;

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

/**
 * ContributionFeeSeeder
 *
 * Seeds one monthly contribution fee per SSHIA membership category.
 * Member contributions form the financial backbone of SSHIA's operational
 * capacity, funding training programmes, research initiatives, advocacy
 * campaigns, and member welfare services. Contribution fees are denominated
 * in US Dollars (USD) and collected on a monthly basis.
 *
 * Founder and Honorary Members are exempt from monthly contributions
 * in recognition of their foundational and honorary status respectively.
 *
 * Depends on: MembershipCategorySeeder
 * Uses firstOrCreate keyed on (membership_category_id, name) for idempotency.
 */
class ContributionFeeSeeder extends Seeder
{
    public function run(): void
    {
        // Map category slug => contribution fee configuration
        $feeConfig = [
            'founder-members' => [
                'name'        => 'Founder Member Monthly Contribution',
                'description' => 'Monthly contribution for Founder Members. Founder Members are exempt from mandatory monthly contributions in recognition of their foundational role in establishing SSHIA. Voluntary contributions are welcomed and gratefully acknowledged.',
                'amount'      => 0.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => true,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'ordinary-members' => [
                'name'        => 'Ordinary Member Monthly Contribution',
                'description' => 'Monthly contribution for Ordinary Members of SSHIA. These contributions directly fund SSHIA\'s capacity-building programmes, research publications, advocacy initiatives, and member welfare services. Contributions are due on the 1st of each month.',
                'amount'      => 5.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => false,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'associate-members' => [
                'name'        => 'Associate Member Monthly Contribution',
                'description' => 'Monthly contribution for Associate Members of SSHIA. Associate Members contribute to the Association\'s programmes and activities, supporting the development of health informatics capacity in South Sudan from an international perspective.',
                'amount'      => 7.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => false,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'honorary-members' => [
                'name'        => 'Honorary Member Monthly Contribution',
                'description' => 'Monthly contribution for Honorary Members. Honorary Members are exempt from mandatory monthly contributions as their membership is conferred on merit. Voluntary contributions are welcomed and gratefully acknowledged.',
                'amount'      => 0.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => true,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'institutional-members' => [
                'name'        => 'Institutional Member Monthly Contribution',
                'description' => 'Monthly contribution for Institutional Members of SSHIA, including corporate organisations, academic institutions, and research centres. Institutional contributions support joint research, co-branded publications, and collaborative advocacy programmes.',
                'amount'      => 20.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => false,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'graduate-members' => [
                'name'        => 'Graduate Member Monthly Contribution',
                'description' => 'Reduced monthly contribution for Graduate Members of SSHIA. This concessionary rate acknowledges the financial constraints faced by emerging health informatics professionals and encourages sustained membership during the critical early career phase.',
                'amount'      => 2.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => false,
                'is_active'   => true,
                'due_day'     => 1,
            ],
            'affiliate-members' => [
                'name'        => 'Affiliate Member Monthly Contribution',
                'description' => 'Monthly contribution for Affiliate Members of SSHIA. Affiliate Members are international organisations that collaborate with SSHIA on health informatics advocacy, research, and knowledge-sharing. Contributions support joint international initiatives.',
                'amount'      => 15.00,
                'currency'    => 'USD',
                'cycle'       => 'monthly',
                'is_optional' => false,
                'is_active'   => true,
                'due_day'     => 1,
            ],
        ];

        $seeded = 0;

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

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

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

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

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

            $seeded++;
        }

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