<?php

namespace Database\Seeders;

use App\Models\Plan;
use App\Models\SaasSubscription;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $freePlan = Plan::query()->updateOrCreate(
            ['slug' => 'free'],
            ['name' => 'Free', 'price_cents' => 0, 'interval' => 'monthly', 'is_active' => true]
        );
        $proPlan = Plan::query()->updateOrCreate(
            ['slug' => 'pro'],
            ['name' => 'Pro', 'price_cents' => 1900, 'interval' => 'monthly', 'is_active' => true]
        );
        $enterprisePlan = Plan::query()->updateOrCreate(
            ['slug' => 'enterprise'],
            ['name' => 'Enterprise', 'price_cents' => 9900, 'interval' => 'monthly', 'is_active' => true]
        );

        $freePlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::CSV_IMPORT],
            ['limit' => 100]
        );
        $proPlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::CSV_IMPORT],
            ['limit' => 1000]
        );
        $proPlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::AI_ADVISOR],
            ['limit' => 200]
        );
        $proPlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::SUBSCRIPTION_SHARING],
            ['limit' => null]
        );
        $enterprisePlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::CSV_IMPORT],
            ['limit' => null]
        );
        $enterprisePlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::AI_ADVISOR],
            ['limit' => null]
        );
        $enterprisePlan->features()->updateOrCreate(
            ['feature_key' => \App\Support\FeatureKey::SUBSCRIPTION_SHARING],
            ['limit' => null]
        );

        $superAdmin = User::query()->updateOrCreate([
            'email' => env('SEED_SUPER_ADMIN_EMAIL', 'admin@example.com'),
        ], [
            'name' => env('SEED_SUPER_ADMIN_NAME', 'Super Admin'),
            'password' => Hash::make(env('SEED_SUPER_ADMIN_PASSWORD', 'ChangeMe123!')),
            'role' => 'super_admin',
            'email_verified_at' => now(),
        ]);

        $user = User::query()->updateOrCreate([
            'email' => 'test@example.com',
        ], [
            'name' => 'Test User',
            'password' => Hash::make('password'),
            'role' => 'user',
            'email_verified_at' => now(),
        ]);

        SaasSubscription::query()->updateOrCreate(
            ['user_id' => $user->id],
            [
                'plan_id' => $proPlan->id,
                'status' => 'active',
                'current_period_start' => now()->startOfMonth(),
                'current_period_end' => now()->endOfMonth(),
            ]
        );

        SaasSubscription::query()->updateOrCreate(
            ['user_id' => $superAdmin->id],
            [
                'plan_id' => $enterprisePlan->id,
                'status' => 'active',
                'current_period_start' => now()->startOfMonth(),
                'current_period_end' => now()->endOfMonth(),
            ]
        );
    }
}
