<?php

namespace Database\Seeders;

use App\Models\LoanProduct;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

/**
 * LoanProductSeeder
 *
 * Seeds the two foundational SSHIA member loan products. These products
 * provide financial support to SSHIA members in good standing, enabling
 * them to address urgent personal needs and invest in professional and
 * business development. Loan products are denominated in South Sudan
 * Pounds (SSP) to serve the local membership base.
 *
 * Uses updateOrCreate keyed on slug for idempotency — safe to re-run.
 */
class LoanProductSeeder extends Seeder
{
    public function run(): void
    {
        $products = [
            [
                'name'                    => 'Emergency Loan',
                'slug'                    => 'emergency-loan',
                'description'             => 'A short-term financial assistance product designed to help SSHIA members in good standing address urgent and unforeseen financial needs. The Emergency Loan provides rapid access to funds with a streamlined approval process, enabling members to respond to medical emergencies, family crises, or other time-sensitive financial obligations without disrupting their professional commitments.',
                'terms_and_conditions'    => "1. Applicant must be an active SSHIA member in good standing with no outstanding loan defaults.\n2. Loan amount is subject to the applicant's contribution balance and repayment capacity assessment.\n3. Repayment commences within 30 days of disbursement.\n4. Early repayment is permitted without penalty.\n5. Failure to repay as agreed may result in suspension of membership benefits.\n6. SSHIA reserves the right to deduct outstanding amounts from member contributions upon default.\n7. All loan applications are subject to approval by the SSHIA Finance Committee.",
                'interest_type'           => 'flat_rate',
                'interest_rate'           => 5.00,
                'interest_rate_display'   => 'per annum (flat rate)',
                'min_amount'              => 100.00,
                'max_amount'              => 5000.00,
                'default_amount'          => 500.00,
                'currency'                => 'SSP',
                'min_term_months'         => 1,
                'max_term_months'         => 12,
                'default_term_months'     => 6,
                'eligible_categories'     => null,
                'min_membership_months'   => 3,
                'min_contribution_balance'=> 0.00,
                'processing_fee'          => 0.00,
                'processing_fee_percentage' => 1.00,
                'late_payment_penalty'    => 2.00,
                'is_active'               => true,
                'is_featured'             => true,
                'requires_collateral'     => false,
                'requires_guarantor'      => false,
                'min_guarantors'          => 0,
                'icon'                    => 'fa-bolt',
                'theme_color'             => 'red',
                'sort_order'              => 1,
            ],
            [
                'name'                    => 'Development Loan',
                'slug'                    => 'development-loan',
                'description'             => 'A medium-term investment loan designed to support SSHIA members in financing professional development, business ventures, health informatics projects, and capacity-building initiatives. The Development Loan empowers members to invest in their careers and enterprises, contributing to the broader growth of the health informatics sector in South Sudan. Larger loan amounts are available to members with a strong contribution history and demonstrated repayment capacity.',
                'terms_and_conditions'    => "1. Applicant must be an active SSHIA member in good standing for a minimum of 6 months.\n2. A detailed loan purpose statement and repayment plan must be submitted with the application.\n3. Loans above SSP 20,000 require at least one guarantor who is also an active SSHIA member.\n4. Repayment commences within 30 days of disbursement on a monthly instalment basis.\n5. Early repayment is permitted without penalty.\n6. The loan may be used for professional development, health informatics projects, or approved business purposes only.\n7. Misuse of loan funds for purposes other than stated in the application may result in immediate recall of the outstanding balance.\n8. All loan applications are subject to approval by the SSHIA Finance Committee and Board of Directors for amounts exceeding SSP 25,000.",
                'interest_type'           => 'reducing_balance',
                'interest_rate'           => 8.00,
                'interest_rate_display'   => 'per annum (reducing balance)',
                'min_amount'              => 1000.00,
                'max_amount'              => 50000.00,
                'default_amount'          => 5000.00,
                'currency'                => 'SSP',
                'min_term_months'         => 6,
                'max_term_months'         => 36,
                'default_term_months'     => 24,
                'eligible_categories'     => null,
                'min_membership_months'   => 6,
                'min_contribution_balance'=> 0.00,
                'processing_fee'          => 0.00,
                'processing_fee_percentage' => 2.00,
                'late_payment_penalty'    => 3.00,
                'is_active'               => true,
                'is_featured'             => true,
                'requires_collateral'     => false,
                'requires_guarantor'      => true,
                'min_guarantors'          => 1,
                'icon'                    => 'fa-chart-line',
                'theme_color'             => 'blue',
                'sort_order'              => 2,
            ],
        ];

        foreach ($products as $data) {
            LoanProduct::updateOrCreate(
                ['slug' => $data['slug']],
                $data
            );
        }

        $this->command->info('LoanProductSeeder: 2 loan products seeded successfully.');
    }
}
