<?php

namespace Database\Seeders;

use App\Modules\Promotions\Models\Coupon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;

/**
 * Seeds 4 test coupons for development / QA.
 *
 * Codes to use at checkout:
 *   FLAT200   — NPR 200 flat off (min order NPR 500)
 *   SAVE10    — 10 % off, capped at NPR 500 (min order NPR 300)
 *   FREESHIP  — Free shipping (any order value)
 *   NEWUSER   — 15 % off, capped at NPR 300, single-use per customer
 */
class CouponSeeder extends Seeder
{
    public function run(): void
    {
        $coupons = [
            [
                'code'        => 'FLAT200',
                'type'        => 'flat',
                'value'       => 200.00,
                'max_discount'=> null,
                'min_order'   => 500.00,
                'max_uses'    => 100,
                'used_count'  => 0,
                'is_active'   => true,
                'starts_at'   => Carbon::now()->subDay(),
                'expires_at'  => Carbon::now()->addYear(),
                'description' => 'NPR 200 flat discount on orders above NPR 500',
            ],
            [
                'code'        => 'SAVE10',
                'type'        => 'percent',
                'value'       => 10.00,
                'max_discount'=> 500.00,
                'min_order'   => 300.00,
                'max_uses'    => 200,
                'used_count'  => 0,
                'is_active'   => true,
                'starts_at'   => Carbon::now()->subDay(),
                'expires_at'  => Carbon::now()->addYear(),
                'description' => '10% off (max NPR 500) on orders above NPR 300',
            ],
            [
                'code'        => 'FREESHIP',
                'type'        => 'free_ship',
                'value'       => 0.00,
                'max_discount'=> null,
                'min_order'   => null,
                'max_uses'    => 500,
                'used_count'  => 0,
                'is_active'   => true,
                'starts_at'   => Carbon::now()->subDay(),
                'expires_at'  => Carbon::now()->addYear(),
                'description' => 'Free shipping on any order',
            ],
            [
                'code'        => 'NEWUSER',
                'type'        => 'percent',
                'value'       => 15.00,
                'max_discount'=> 300.00,
                'min_order'   => null,
                'max_uses'    => 1000,
                'used_count'  => 0,
                'is_active'   => true,
                'starts_at'   => Carbon::now()->subDay(),
                'expires_at'  => Carbon::now()->addYear(),
                'description' => '15% off for new customers (max NPR 300)',
            ],
        ];

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

        $this->command->info('✔ Coupons seeded: FLAT200 | SAVE10 | FREESHIP | NEWUSER');
    }
}
