<?php

namespace Database\Seeders;

use App\Enums\CouponStatus;
use App\Enums\CouponType;
use App\Models\Coupon;
use App\Models\Package;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;

class CouponSeeder extends Seeder
{
    /**
     * Seed a curated set of coupons and attach them to the relevant packages.
     */
    public function run(): void
    {
        $startsAt = Carbon::create(2026, 1, 1, 0, 0, 0);

        /** @var Collection<string, int> $packageIds */
        $packageIds = Package::query()
            ->whereNotNull('paddle_price_id')
            ->pluck('id', 'slug');

        $coupons = [
            [
                'code' => 'LAUNCH10',
                'name' => 'Launch 10% Evergreen',
                'description' => 'Dauerhafter 10%-Rabatt auf alle Endkunden-Pakete für Kampagnen und Empfehlungen.',
                'type' => CouponType::PERCENTAGE,
                'amount' => 10,
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => null,
                'per_customer_limit' => null,
                'starts_at' => $startsAt,
                'ends_at' => null,
                'packages' => ['starter', 'standard', 'pro', 'premium'],
            ],
            [
                'code' => 'WELCOME20',
                'name' => 'Willkommen 20%',
                'description' => 'Einmaliger 20%-Rabatt für neue Kund:innen auf Endkunden-Pakete.',
                'type' => CouponType::PERCENTAGE,
                'amount' => 20,
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => 500,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2026, 12, 31, 23, 59, 59),
                'packages' => ['starter', 'standard', 'pro', 'premium'],
            ],
            [
                'code' => 'UPGRADE30',
                'name' => 'Upgrade 30 €',
                'description' => '30 € Nachlass als Upgrade-Anreiz von Starter auf Standard/Premium.',
                'type' => CouponType::FLAT,
                'amount' => 30.00,
                'currency' => 'EUR',
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => 200,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2026, 9, 30, 23, 59, 59),
                'packages' => ['standard', 'pro', 'premium'],
            ],
            [
                'code' => 'SEASON50',
                'name' => 'Hochzeits-Saison 50 €',
                'description' => 'Saisonaler 50 € Rabatt für die Hochzeitssaison auf Standard/Premium.',
                'type' => CouponType::FLAT,
                'amount' => 50.00,
                'currency' => 'EUR',
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => 300,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2026, 8, 31, 23, 59, 59),
                'packages' => ['standard', 'pro', 'premium'],
            ],
            [
                'code' => 'B2B15',
                'name' => 'Partner 15 %',
                'description' => '15 %-Rabatt für Reseller-Pakete als Partner-Pitch.',
                'type' => CouponType::PERCENTAGE,
                'amount' => 15,
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => null,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2027, 12, 31, 23, 59, 59),
                'packages' => ['s-small-reseller', 'm-medium-reseller', 'l-large-reseller', 'studio-annual', 'enterprise-unlimited'],
            ],
            [
                'code' => 'B2B100',
                'name' => 'Partner 100 €',
                'description' => '100 € Closing-Rabatt für mittlere/große Reseller-Pakete.',
                'type' => CouponType::FLAT,
                'amount' => 100.00,
                'currency' => 'EUR',
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => 100,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2026, 12, 31, 23, 59, 59),
                'packages' => ['m-medium-reseller', 'l-large-reseller', 'studio-annual'],
            ],
            [
                'code' => 'ENTERPRISE500',
                'name' => 'Enterprise 500 €',
                'description' => '500 € Einmalrabatt für Enterprise/Unlimited-Deals.',
                'type' => CouponType::FLAT,
                'amount' => 500.00,
                'currency' => 'EUR',
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => false,
                'usage_limit' => 30,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2027, 3, 31, 23, 59, 59),
                'packages' => ['enterprise-unlimited'],
            ],
            [
                'code' => 'RECOVER10',
                'name' => 'Recover 10 %',
                'description' => '10 %-Warenkorbrabatt für Rückgewinnung via Remarketing-Link (auto-apply).',
                'type' => CouponType::PERCENTAGE,
                'amount' => 10,
                'status' => CouponStatus::ACTIVE,
                'enabled_for_checkout' => true,
                'is_stackable' => false,
                'auto_apply' => true,
                'usage_limit' => 1000,
                'per_customer_limit' => 1,
                'starts_at' => $startsAt,
                'ends_at' => Carbon::create(2026, 12, 31, 23, 59, 59),
                'packages' => ['starter', 'standard', 'pro', 'premium'],
            ],
        ];

        foreach ($coupons as $data) {
            $packageSlugs = $data['packages'] ?? [];
            unset($data['packages']);

            $coupon = Coupon::updateOrCreate(
                ['code' => $data['code']],
                $data,
            );

            $ids = collect($packageSlugs)
                ->map(fn (string $slug) => $packageIds->get($slug))
                ->filter()
                ->values()
                ->all();

            $coupon->packages()->sync($ids);
        }
    }
}
