<?php

namespace Database\Seeders;

use App\Modules\Admin\Tenancy\Models\Module;
use App\Modules\Admin\Tenancy\Models\Plan;
use App\Modules\Admin\Tenancy\Models\PlanLimit;
use App\Modules\Admin\Tenancy\Services\CustomFieldsPlanService;
use App\Modules\Admin\Tenancy\Support\PlanLimitKeys;
use Illuminate\Database\Seeder;

class TenancySeeder extends Seeder
{
    public function run(): void
    {
        $modules = [
            ['slug' => 'hr', 'name' => 'Human Resources', 'description' => 'Manage people, departments and roles.', 'icon' => 'fe fe-users'],
            ['slug' => 'sales', 'name' => 'Sales', 'description' => 'Leads, deals and pipelines.', 'icon' => 'fe fe-trending-up'],
            ['slug' => 'finance', 'name' => 'Finance', 'description' => 'Accounting and billing.', 'icon' => 'fe fe-dollar-sign'],
            ['slug' => 'crm', 'name' => 'CRM', 'description' => 'Customer relationship management.', 'icon' => 'fe fe-user-check'],
            ['slug' => 'inventory', 'name' => 'Inventory', 'description' => 'Stock, warehouses and products.', 'icon' => 'fe fe-package'],
        ];

        foreach ($modules as $module) {
            Module::query()->updateOrCreate(
                ['slug' => $module['slug']],
                $module + ['is_active' => true],
            );
        }

        $trialPlan = Plan::query()->updateOrCreate(
            ['slug' => 'trial'],
            [
                'name' => 'Trial',
                'description' => 'Free trial with access to all core modules.',
                'price' => 0,
                'currency' => 'USD',
                'billing_cycle' => 'monthly',
                'trial_days' => 14,
                'is_trial' => true,
                'is_default' => true,
                'is_active' => true,
            ],
        );

        $basicPlan = Plan::query()->updateOrCreate(
            ['slug' => 'basic'],
            [
                'name' => 'Basic',
                'description' => 'Core HR + CRM modules for small teams.',
                'price' => 29.00,
                'currency' => 'USD',
                'billing_cycle' => 'monthly',
                'trial_days' => 0,
                'is_trial' => false,
                'is_default' => false,
                'is_active' => true,
            ],
        );

        $proPlan = Plan::query()->updateOrCreate(
            ['slug' => 'pro'],
            [
                'name' => 'Pro',
                'description' => 'All modules included.',
                'price' => 99.00,
                'currency' => 'USD',
                'billing_cycle' => 'monthly',
                'trial_days' => 0,
                'is_trial' => false,
                'is_default' => false,
                'is_active' => true,
            ],
        );

        $enterprisePlan = Plan::query()->updateOrCreate(
            ['slug' => 'enterprise'],
            [
                'name' => 'Enterprise',
                'description' => 'Unlimited scale and customization.',
                'price' => 299.00,
                'currency' => 'USD',
                'billing_cycle' => 'yearly',
                'trial_days' => 0,
                'is_trial' => false,
                'is_default' => false,
                'is_active' => true,
            ],
        );

        $all = Module::query()->pluck('id', 'slug');

        $trialPlan->modules()->sync($all->values()->all());
        $basicPlan->modules()->sync([$all['hr'], $all['crm']]);
        $proPlan->modules()->sync($all->values()->all());
        $enterprisePlan->modules()->sync($all->values()->all());

        $this->seedPlanLimits($trialPlan, $basicPlan, $proPlan, $enterprisePlan);
    }

    private function seedPlanLimits(Plan $trial, Plan $basic, Plan $pro, Plan $enterprise): void
    {
        $cfFeature = CustomFieldsPlanService::LIMIT_KEY;

        foreach ([$trial, $basic] as $plan) {
            PlanLimit::query()->updateOrCreate(
                ['plan_id' => $plan->id, 'key' => $cfFeature],
                ['is_enabled' => false, 'limit' => null],
            );
            PlanLimit::query()->updateOrCreate(
                ['plan_id' => $plan->id, 'key' => PlanLimitKeys::CUSTOM_FIELDS_LIMIT],
                ['is_enabled' => false, 'limit' => null],
            );
        }

        PlanLimit::query()->updateOrCreate(
            ['plan_id' => $pro->id, 'key' => $cfFeature],
            ['is_enabled' => true, 'limit' => null],
        );
        PlanLimit::query()->updateOrCreate(
            ['plan_id' => $pro->id, 'key' => PlanLimitKeys::CUSTOM_FIELDS_LIMIT],
            ['is_enabled' => true, 'limit' => 10],
        );

        PlanLimit::query()->updateOrCreate(
            ['plan_id' => $enterprise->id, 'key' => $cfFeature],
            ['is_enabled' => true, 'limit' => null],
        );
        PlanLimit::query()->updateOrCreate(
            ['plan_id' => $enterprise->id, 'key' => PlanLimitKeys::CUSTOM_FIELDS_LIMIT],
            ['is_enabled' => true, 'limit' => null],
        );

        $employeeCaps = [
            $trial->id => 5,
            $basic->id => 25,
            $pro->id => 100,
            $enterprise->id => null,
        ];
        foreach ($employeeCaps as $planId => $limit) {
            PlanLimit::query()->updateOrCreate(
                ['plan_id' => $planId, 'key' => PlanLimitKeys::EMPLOYEES_LIMIT],
                ['is_enabled' => true, 'limit' => $limit],
            );
        }

        $attendanceHistory = [
            $trial->id => 6,
            $basic->id => 12,
            $pro->id => 24,
            $enterprise->id => 60,
        ];
        foreach ($attendanceHistory as $planId => $months) {
            PlanLimit::query()->updateOrCreate(
                ['plan_id' => $planId, 'key' => PlanLimitKeys::ATTENDANCE_HISTORY_MONTHS],
                ['is_enabled' => true, 'limit' => $months],
            );
        }
    }
}
