<?php

namespace Database\Seeders;

use App\Models\Coupon;
use App\Models\CouponCriteria;
use App\Models\Currency;
use App\Models\Product;
use App\Models\Promotion;
use Illuminate\Database\Seeder;

class CouponSeeder extends Seeder
{
    public function run(): void
    {
        // QA fixtures (PRC-532): 3 kupon + 1 promosyon.
        CouponCriteria::query()->delete();
        Coupon::query()->delete();
        Promotion::query()->delete();

        $tryId = Currency::where('code', 'TRY')->value('id');

        // --- Kuponlar ---
        Coupon::create([
            'code'           => 'QA10',
            'name'           => 'QA %10 İndirim',
            'description'    => 'QA test kuponu — tüm ürünlerde %10',
            'type'           => 'percentage',
            'discount_value' => 10,
            'currency_id'    => $tryId,
            'min_order_amount' => 0,
            'usage_limit'    => null,
            'is_active'      => true,
            'starts_at'      => now(),
            'expires_at'     => null, // süresiz (spec §12)
            'target_type'    => 'all',
        ]);

        Coupon::create([
            'code'           => 'QA50',
            'name'           => 'QA 50 TRY İndirim',
            'description'    => 'QA test kuponu — sabit 50 TRY',
            'type'           => 'fixed',
            'discount_value' => 50,
            'currency_id'    => $tryId,
            'min_order_amount' => 0,
            'usage_limit'    => null,
            'is_active'      => true,
            'starts_at'      => now(),
            'expires_at'     => now()->addYear(),
            'target_type'    => 'all',
        ]);

        Coupon::create([
            'code'           => 'QAEXPIRED',
            'name'           => 'QA Süresi Geçmiş Kupon',
            'description'    => 'QA negatif test — süresi geçmiş',
            'type'           => 'percentage',
            'discount_value' => 30,
            'currency_id'    => $tryId,
            'min_order_amount' => 0,
            'usage_limit'    => null,
            'is_active'      => true,
            'starts_at'      => now()->subYears(2),
            'expires_at'     => now()->subYear(),
            'target_type'    => 'all',
        ]);

        // --- Promosyon (oto-uygulanan) — hedef: QA-PRO ---
        $qaProId = Product::where('slug', 'qa-pro')->value('id');
        Promotion::create([
            'code'            => 'QAPROMO15',
            'description'     => 'QA test promosyonu — QA-PRO %15',
            'status'          => 'active',
            'type'            => 'percentage',
            'amount'          => 15,
            'target_type'     => 'specific_products',
            'target_products' => $qaProId ? [$qaProId] : [],
        ]);
    }
}
