<?php

namespace Database\Seeders\Demo;

use App\Models\Customers\Customer;
use App\Models\Lookups\DiscountType;
use App\Models\Lookups\Status;
use App\Models\Lookups\VoucherType;
use App\Models\Loyalty\LoyaltyProgram;
use App\Models\Loyalty\LoyaltyTier;
use App\Models\Vouchers\Voucher;
use Illuminate\Database\Seeder;

class LoyaltyVoucherSeeder extends Seeder
{
    public function run(): void
    {
        $program = LoyaltyProgram::create([
            'name' => 'Standard Rewards',
            'base_earn_rate' => 1,
            'redemption_rate' => 100,
            'minimum_purchase_to_earn' => 0,
            'minimum_points_to_redeem' => 100,
            'points_validity_days' => 365,
            'is_active' => true,
            'started_at' => now()->subMonths(8)->toDateString(),
        ]);

        LoyaltyTier::create(['program_id' => $program->id, 'name' => 'Bronze', 'code' => 'bronze', 'min_lifetime_points' => 0, 'earn_multiplier' => 1, 'sort_order' => 10]);
        LoyaltyTier::create(['program_id' => $program->id, 'name' => 'Silver', 'code' => 'silver', 'min_lifetime_points' => 5000, 'earn_multiplier' => 1.25, 'sort_order' => 20]);
        LoyaltyTier::create(['program_id' => $program->id, 'name' => 'Gold', 'code' => 'gold', 'min_lifetime_points' => 20000, 'earn_multiplier' => 1.5, 'sort_order' => 30]);

        $activeId = Status::idOf(Status::CTX_VOUCHER, Status::VOUCHER_ACTIVE);
        $expiredId = Status::idOf(Status::CTX_VOUCHER, Status::VOUCHER_EXPIRED);
        $voidId = Status::idOf(Status::CTX_VOUCHER, Status::VOUCHER_VOID);

        $customers = Customer::inRandomOrder()->limit(4)->get();

        // Discount vouchers
        Voucher::create(['code' => 'WELCOME200', 'name' => 'Welcome Discount', 'voucher_type_id' => VoucherType::idOf(VoucherType::DISCOUNT), 'discount_type_id' => DiscountType::idOf(DiscountType::FIXED), 'discount_value' => 200, 'minimum_purchase' => 1000, 'status_id' => $activeId, 'is_stackable' => true, 'valid_until' => now()->addMonths(3)]);
        Voucher::create(['code' => 'FLAT500', 'name' => 'Flat 500 Off', 'voucher_type_id' => VoucherType::idOf(VoucherType::DISCOUNT), 'discount_type_id' => DiscountType::idOf(DiscountType::FIXED), 'discount_value' => 500, 'minimum_purchase' => 3000, 'status_id' => $activeId, 'is_stackable' => false, 'valid_until' => now()->addMonths(2)]);
        Voucher::create(['code' => 'SAVE10', 'name' => 'Save 10%', 'voucher_type_id' => VoucherType::idOf(VoucherType::DISCOUNT), 'discount_type_id' => DiscountType::idOf(DiscountType::PERCENTAGE), 'discount_value' => 10, 'maximum_discount' => 500, 'status_id' => $activeId, 'is_stackable' => true, 'valid_until' => now()->addMonths(4)]);
        Voucher::create(['code' => 'OLDPROMO', 'name' => 'Expired Seasonal Promo', 'voucher_type_id' => VoucherType::idOf(VoucherType::DISCOUNT), 'discount_type_id' => DiscountType::idOf(DiscountType::FIXED), 'discount_value' => 300, 'status_id' => $expiredId, 'valid_from' => now()->subMonths(4), 'valid_until' => now()->subMonths(2)]);

        // Promotional vouchers
        Voucher::create(['code' => 'NEWYEAR15', 'name' => 'New Year 15% Off', 'voucher_type_id' => VoucherType::idOf(VoucherType::PROMOTIONAL), 'discount_type_id' => DiscountType::idOf(DiscountType::PERCENTAGE), 'discount_value' => 15, 'status_id' => $activeId, 'is_stackable' => false, 'valid_until' => now()->addMonths(1)]);
        Voucher::create(['code' => 'FLASH20', 'name' => 'Flash Sale 20%', 'voucher_type_id' => VoucherType::idOf(VoucherType::PROMOTIONAL), 'discount_type_id' => DiscountType::idOf(DiscountType::PERCENTAGE), 'discount_value' => 20, 'maximum_discount' => 1000, 'status_id' => $activeId, 'is_stackable' => false, 'usage_limit' => 50, 'valid_until' => now()->addWeeks(2)]);
        Voucher::create(['code' => 'CANCELLED50', 'name' => 'Cancelled Promo', 'voucher_type_id' => VoucherType::idOf(VoucherType::PROMOTIONAL), 'discount_type_id' => DiscountType::idOf(DiscountType::FIXED), 'discount_value' => 50, 'status_id' => $voidId]);

        // Gift vouchers (balance-based)
        Voucher::create(['code' => 'GIFT500', 'name' => 'Gift Card LKR 500', 'voucher_type_id' => VoucherType::idOf(VoucherType::GIFT), 'initial_balance' => 500, 'remaining_balance' => 500, 'is_partial_redemption' => true, 'status_id' => $activeId, 'valid_until' => now()->addYear()]);
        Voucher::create(['code' => 'GIFT1000', 'name' => 'Gift Card LKR 1,000', 'voucher_type_id' => VoucherType::idOf(VoucherType::GIFT), 'initial_balance' => 1000, 'remaining_balance' => 1000, 'is_partial_redemption' => true, 'status_id' => $activeId, 'valid_until' => now()->addYear()]);
        Voucher::create(['code' => 'GIFT2500', 'name' => 'Gift Card LKR 2,500', 'voucher_type_id' => VoucherType::idOf(VoucherType::GIFT), 'initial_balance' => 2500, 'remaining_balance' => 2500, 'is_partial_redemption' => true, 'status_id' => $activeId, 'valid_until' => now()->addYear()]);

        // Refund vouchers (customer-specific, from CS/returns flows)
        if ($customers->count() >= 2) {
            Voucher::create(['code' => 'REFUND-'.strtoupper(fake()->bothify('??####')), 'name' => 'Return Refund Voucher', 'voucher_type_id' => VoucherType::idOf(VoucherType::REFUND), 'initial_balance' => 750, 'remaining_balance' => 750, 'is_partial_redemption' => true, 'customer_id' => $customers[0]->id, 'status_id' => $activeId, 'valid_until' => now()->addMonths(6)]);
            Voucher::create(['code' => 'REFUND-'.strtoupper(fake()->bothify('??####')), 'name' => 'Return Refund Voucher', 'voucher_type_id' => VoucherType::idOf(VoucherType::REFUND), 'initial_balance' => 1200, 'remaining_balance' => 1200, 'is_partial_redemption' => true, 'customer_id' => $customers[1]->id, 'status_id' => $activeId, 'valid_until' => now()->addMonths(6)]);
        }

        // Compensation vouchers (customer-specific, goodwill gestures)
        if ($customers->count() >= 4) {
            Voucher::create(['code' => 'SORRY-'.strtoupper(fake()->bothify('??####')), 'name' => 'Service Recovery Voucher', 'voucher_type_id' => VoucherType::idOf(VoucherType::COMPENSATION), 'initial_balance' => 1000, 'remaining_balance' => 1000, 'is_partial_redemption' => true, 'customer_id' => $customers[2]->id, 'status_id' => $activeId, 'valid_until' => now()->addMonths(3)]);
            Voucher::create(['code' => 'SORRY-'.strtoupper(fake()->bothify('??####')), 'name' => 'Service Recovery Voucher', 'voucher_type_id' => VoucherType::idOf(VoucherType::COMPENSATION), 'initial_balance' => 500, 'remaining_balance' => 500, 'is_partial_redemption' => true, 'customer_id' => $customers[3]->id, 'status_id' => $activeId, 'valid_until' => now()->addMonths(3)]);
        }
    }
}
