<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\PointRule;
use App\Models\Tenant;
use Illuminate\Database\Seeder;

class DefaultPointRulesSeeder extends Seeder
{
    public const RULES = [
        ['post_create',     20,  100],
        ['comment_create',   5,   50],
        ['post_liked',       3,  null],
        ['comment_liked',    1,  null],
        ['login_daily',      5,    5],
        ['first_purchase', 500,  null],
        ['referral',       200,  null],
        ['birthday',      1000,  null],
    ];

    public function run(): void
    {
        $tenants = Tenant::query()->get();
        if ($tenants->isEmpty()) {
            $this->command?->warn('No tenants found — skipping PointRuleSeeder.');
            return;
        }

        foreach ($tenants as $tenant) {
            foreach (self::RULES as [$action, $points, $cap]) {
                PointRule::query()->updateOrCreate(
                    ['tenant_id' => $tenant->id, 'action' => $action],
                    [
                        'points' => $points,
                        'cap_per_day' => $cap,
                        'is_active' => true,
                    ],
                );
            }
        }
    }
}
