<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Coupon;
use Carbon\Carbon;
use Illuminate\Support\Str;

class CouponSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Clear existing coupons to avoid duplicates if needed, or just append.
        // Coupon::truncate(); // Optional, better not to wipe user data if any.

        $coupons = [
            [
                'code' => 'WELCOME2024',
                'type' => 'percent',
                'value' => 10,
                'min_purchase' => 0,
                'is_active' => true,
                'usage_limit' => null,
                'used_count' => 150,
                'expires_at' => Carbon::now()->addMonths(6),
            ],
            [
                'code' => 'SAVE50',
                'type' => 'fixed',
                'value' => 50,
                'min_purchase' => 200,
                'is_active' => true,
                'usage_limit' => 100,
                'used_count' => 45,
                'expires_at' => Carbon::now()->addDays(7),
            ],
            [
                'code' => 'FREESHIP',
                'type' => 'fixed',
                'value' => 15, // Assuming 15 is shipping cost
                'min_purchase' => 50,
                'is_active' => true,
                'usage_limit' => 500,
                'used_count' => 120,
                'expires_at' => Carbon::now()->addYear(),
            ],
            [
                'code' => 'FLASH25',
                'type' => 'percent',
                'value' => 25,
                'min_purchase' => 100,
                'is_active' => true,
                'usage_limit' => 50,
                'used_count' => 48,
                'expires_at' => Carbon::now()->addHours(12),
            ],
            [
                'code' => 'SUMMERVIBES',
                'type' => 'percent',
                'value' => 15,
                'min_purchase' => 50,
                'is_active' => false, // Inactive
                'usage_limit' => null,
                'used_count' => 12,
                'expires_at' => Carbon::now()->addMonth(),
            ],
            [
                'code' => 'EXPIRED2023',
                'type' => 'fixed',
                'value' => 20,
                'min_purchase' => 100,
                'is_active' => true,
                'usage_limit' => null,
                'used_count' => 300,
                'expires_at' => Carbon::now()->subDays(10), // Expired
            ],
        ];

        // Generate 14 more random coupons
        for ($i = 0; $i < 14; $i++) {
            $type = rand(0, 1) ? 'percent' : 'fixed';
            $value = $type == 'percent' ? rand(5, 50) : rand(10, 100);
            $isActive = rand(0, 10) > 2; // Mostly active
            
            $coupons[] = [
                'code' => strtoupper(Str::random(8)),
                'type' => $type,
                'value' => $value,
                'min_purchase' => rand(0, 1) ? rand(50, 200) : 0,
                'is_active' => $isActive,
                'usage_limit' => rand(0, 1) ? rand(10, 1000) : null,
                'used_count' => rand(0, 50),
                'expires_at' => rand(0, 1) ? Carbon::now()->addDays(rand(1, 90)) : null,
            ];
        }

        foreach ($coupons as $coupon) {
            Coupon::updateOrCreate(
                ['code' => $coupon['code']],
                $coupon
            );
        }
    }
}
