<?php

namespace Database\Seeders;

use App\Models\Voucher;
use Illuminate\Database\Seeder;

/**
 * Seeds default voucher codes that are tracked in the admin panel.
 *
 * Codes seeded here are redeemable via the API and fully tracked
 * (usage count, per-user deduplication). They are distinct from the
 * offline-only hardcoded codes in subscription_screen.dart.
 *
 * Run: php artisan db:seed --class=VoucherSeeder
 */
class VoucherSeeder extends Seeder
{
    /** @var array<array{code:string,plan:string,period:string,days:int,max_uses:int,notes:string}> */
    private array $vouchers = [
        // ── Growth plan ───────────────────────────────────────────────────
        ['code' => 'QPOS-GRW-M',   'plan' => 'growth', 'period' => 'monthly',  'days' => 30,  'max_uses' => 9999, 'notes' => 'Standard Growth Monthly promo'],
        ['code' => 'QPOS-GRW-Y',   'plan' => 'growth', 'period' => 'yearly',   'days' => 365, 'max_uses' => 9999, 'notes' => 'Standard Growth Yearly promo'],
        ['code' => 'QPOS-GRW-LT',  'plan' => 'growth', 'period' => 'lifetime', 'days' => 0,   'max_uses' => 9999, 'notes' => 'Standard Growth Lifetime promo'],

        // ── Pro plan ──────────────────────────────────────────────────────
        ['code' => 'QPOS-PRO-M',   'plan' => 'pro',    'period' => 'monthly',  'days' => 30,  'max_uses' => 9999, 'notes' => 'Standard Pro Monthly promo'],
        ['code' => 'QPOS-PRO-Y',   'plan' => 'pro',    'period' => 'yearly',   'days' => 365, 'max_uses' => 9999, 'notes' => 'Standard Pro Yearly promo'],
        ['code' => 'QPOS-PRO-LT',  'plan' => 'pro',    'period' => 'lifetime', 'days' => 0,   'max_uses' => 9999, 'notes' => 'Standard Pro Lifetime promo'],

        // ── Demo / testing (single-use) ───────────────────────────────────
        ['code' => 'DEMO-GRW-Y1',  'plan' => 'growth', 'period' => 'yearly',   'days' => 365, 'max_uses' => 1, 'notes' => 'Demo Growth Yearly — single use'],
        ['code' => 'DEMO-GRW-Y2',  'plan' => 'growth', 'period' => 'yearly',   'days' => 365, 'max_uses' => 1, 'notes' => 'Demo Growth Yearly — single use'],
        ['code' => 'DEMO-PRO-Y1',  'plan' => 'pro',    'period' => 'yearly',   'days' => 365, 'max_uses' => 1, 'notes' => 'Demo Pro Yearly — single use'],
        ['code' => 'DEMO-PRO-Y2',  'plan' => 'pro',    'period' => 'yearly',   'days' => 365, 'max_uses' => 1, 'notes' => 'Demo Pro Yearly — single use'],
        ['code' => 'DEMO-PRO-LT1', 'plan' => 'pro',    'period' => 'lifetime', 'days' => 0,   'max_uses' => 1, 'notes' => 'Demo Pro Lifetime — single use'],
    ];

    public function run(): void
    {
        foreach ($this->vouchers as $data) {
            Voucher::updateOrCreate(
                ['code' => $data['code']],
                [
                    'plan'      => $data['plan'],
                    'period'    => $data['period'],
                    'days'      => $data['days'],
                    'max_uses'  => $data['max_uses'],
                    'used_count' => 0,
                    'is_active' => true,
                    'notes'     => $data['notes'],
                    'expires_at' => null, // voucher code itself never expires
                ]
            );
        }
    }
}
