<?php

namespace Database\Seeders;

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

class VoucherSeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::where('is_admin', true)->first();

        $vouchers = [
            [
                'code'           => 'WELCOME10',
                'name'           => 'Selamat Datang 10%',
                'description'    => 'Diskon 10% untuk pelanggan baru',
                'type'           => 'percentage',
                'discount_value' => 10,
                'min_purchase'   => 50000,
                'max_discount'   => 25000,
                'max_uses'       => 100,
                'is_active'      => true,
            ],
            [
                'code'           => 'HEMAT50K',
                'name'           => 'Hemat 50 Ribu',
                'description'    => 'Potongan langsung Rp 50.000',
                'type'           => 'fixed',
                'discount_value' => 50000,
                'min_purchase'   => 200000,
                'max_discount'   => null,
                'max_uses'       => 50,
                'is_active'      => true,
            ],
            [
                'code'           => 'FLASH25',
                'name'           => 'Flash Sale 25%',
                'description'    => 'Diskon kilat 25% hari ini saja',
                'type'           => 'percentage',
                'discount_value' => 25,
                'min_purchase'   => 100000,
                'max_discount'   => 75000,
                'max_uses'       => 30,
                'valid_until'    => now()->addDays(7),
                'is_active'      => true,
            ],
            [
                'code'           => 'GRATIS-ONGKIR',
                'name'           => 'Gratis Ongkos Kirim',
                'description'    => 'Potongan ongkos kirim sampai Rp 30.000',
                'type'           => 'fixed',
                'discount_value' => 30000,
                'min_purchase'   => 0,
                'is_active'      => true,
            ],
        ];

        foreach ($vouchers as $data) {
            $data['created_by'] = $admin?->id;
            Voucher::updateOrCreate(['code' => $data['code']], $data);
        }

        $this->command->info('Sample vouchers created.');
    }
}
