<?php

namespace Tests\Feature\Api;

use App\Models\User;
use App\Modules\Subscription\Models\Feature;
use App\Modules\Subscription\Models\Plan;
use App\Modules\Tenant\Models\Company;
use Database\Seeders\SaasCoreSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class SuperAdminApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_non_super_admin_cannot_access_super_admin_api(): void
    {
        Sanctum::actingAs(User::factory()->create());

        $this->getJson('/api/v1/super-admin/summary')
            ->assertForbidden()
            ->assertJsonPath('message', 'Super admin access required');
    }

    public function test_super_admin_can_view_summary(): void
    {
        $this->seed(SaasCoreSeeder::class);
        $admin = User::query()->where('email', 'admin@biznovia.test')->firstOrFail();

        Sanctum::actingAs($admin);

        $this->getJson('/api/v1/super-admin/summary')
            ->assertOk()
            ->assertJsonPath('success', true)
            ->assertJsonStructure([
                'data' => [
                    'companies' => ['total', 'active'],
                    'subscriptions' => ['trialing', 'active', 'past_due', 'cancelled'],
                    'plans' => ['total', 'active'],
                    'monthly_recurring_revenue',
                ],
            ]);
    }

    public function test_super_admin_can_create_plan_with_features(): void
    {
        $this->seed(SaasCoreSeeder::class);
        $admin = User::query()->where('email', 'admin@biznovia.test')->firstOrFail();
        $invoiceFeature = Feature::query()->where('slug', 'invoice')->firstOrFail();

        Sanctum::actingAs($admin);

        $this->postJson('/api/v1/super-admin/plans', [
            'name' => 'Growth',
            'monthly_price' => 3990,
            'yearly_price' => 39900,
            'feature_ids' => [$invoiceFeature->id],
        ])
            ->assertCreated()
            ->assertJsonPath('data.slug', 'growth')
            ->assertJsonPath('data.features.0.slug', 'invoice');

        $this->assertDatabaseHas('plans', [
            'slug' => 'growth',
        ]);
    }

    public function test_super_admin_can_toggle_feature(): void
    {
        $this->seed(SaasCoreSeeder::class);
        $admin = User::query()->where('email', 'admin@biznovia.test')->firstOrFail();
        $feature = Feature::query()->where('slug', 'ai')->firstOrFail();

        Sanctum::actingAs($admin);

        $this->patchJson("/api/v1/super-admin/features/{$feature->id}", [
            'is_active' => false,
        ])
            ->assertOk()
            ->assertJsonPath('data.is_active', false);
    }

    public function test_super_admin_can_update_subscription_status(): void
    {
        $this->seed(SaasCoreSeeder::class);
        $admin = User::query()->where('email', 'admin@biznovia.test')->firstOrFail();
        $plan = Plan::query()->where('slug', 'starter')->firstOrFail();
        $company = Company::query()->create([
            'name' => 'ABC Grocery',
            'slug' => 'abc-grocery',
            'status' => 'active',
        ]);
        $subscription = $company->subscriptions()->create([
            'plan_id' => $plan->id,
            'billing_cycle' => 'monthly',
            'status' => 'trialing',
            'starts_at' => now(),
        ]);

        Sanctum::actingAs($admin);

        $this->patchJson("/api/v1/super-admin/subscriptions/{$subscription->id}", [
            'status' => 'active',
        ])
            ->assertOk()
            ->assertJsonPath('data.status', 'active');
    }
}
