<?php

namespace Tests\Feature\Subscription;

use App\Models\Members\User;
use App\Models\Members\UserTenant;
use App\Models\Tenants\Plan;
use App\Models\Tenants\Subscription;
use App\Models\Tenants\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class SubscriptionApiTest extends TestCase
{
    use DatabaseTransactions;

    private Tenant $tenant;

    private User $user;

    private Plan $plan;

    private string $apiKey;

    private string $token;

    protected function setUp(): void
    {
        parent::setUp();

        // 테스트용 API Key 생성
        $this->apiKey = 'test-api-key-'.uniqid();
        \DB::table('api_keys')->insert([
            'key' => $this->apiKey,
            'description' => 'Test API Key',
            'is_active' => true,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        // Tenant 생성 또는 기존 사용
        $this->tenant = Tenant::first() ?? Tenant::withoutEvents(function () {
            return Tenant::create([
                'company_name' => 'Test Company',
                'code' => 'TEST'.uniqid(),
                'email' => 'test@example.com',
                'phone' => '010-1234-5678',
            ]);
        });

        // Plan 생성
        $this->plan = Plan::firstOrCreate(
            ['code' => 'TEST_BASIC'],
            [
                'name' => 'Basic Plan',
                'description' => 'Test basic plan',
                'price' => 10000,
                'billing_cycle' => Plan::BILLING_MONTHLY,
                'is_active' => true,
            ]
        );

        // User 생성
        $testUserId = 'testuser'.uniqid();
        $this->user = User::create([
            'user_id' => $testUserId,
            'name' => 'Test User',
            'email' => $testUserId.'@example.com',
            'password' => bcrypt('password123'),
        ]);

        // UserTenant 관계 생성
        UserTenant::create([
            'user_id' => $this->user->id,
            'tenant_id' => $this->tenant->id,
            'is_active' => true,
            'is_default' => true,
        ]);

        // 로그인 및 토큰 획득
        $this->loginAndGetToken();
    }

    protected function loginAndGetToken(): void
    {
        $response = $this->withHeaders([
            'X-API-KEY' => $this->apiKey,
            'Accept' => 'application/json',
        ])->postJson('/api/v1/login', [
            'user_id' => $this->user->user_id,
            'user_pwd' => 'password123',
        ]);

        $response->assertStatus(200);
        $this->token = $response->json('access_token');
    }

    protected function authenticatedRequest(string $method, string $uri, array $data = [])
    {
        return $this->withHeaders([
            'X-API-KEY' => $this->apiKey,
            'Authorization' => 'Bearer '.$this->token,
            'Accept' => 'application/json',
        ])->{$method.'Json'}($uri, $data);
    }

    // ==================== Subscription List Tests ====================

    public function test_can_list_subscriptions(): void
    {
        // 구독 생성
        Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('get', '/api/v1/subscriptions');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data',
            ]);
    }

    public function test_can_get_current_subscription(): void
    {
        // 활성 구독 생성
        Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now()->subDay(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('get', '/api/v1/subscriptions/current');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data',
            ]);
    }

    public function test_can_get_usage(): void
    {
        $response = $this->authenticatedRequest('get', '/api/v1/subscriptions/usage');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data',
            ]);
    }

    // ==================== Subscription CRUD Tests ====================

    public function test_can_create_subscription(): void
    {
        $response = $this->authenticatedRequest('post', '/api/v1/subscriptions', [
            'plan_id' => $this->plan->id,
            'started_at' => now()->format('Y-m-d H:i:s'),
        ]);

        // 201 또는 200 (서비스 미구현 시 500)
        $this->assertContains($response->status(), [200, 201, 500]);
    }

    public function test_can_show_subscription(): void
    {
        $subscription = Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('get', "/api/v1/subscriptions/{$subscription->id}");

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data',
            ]);
    }

    // ==================== Subscription Action Tests ====================

    public function test_can_cancel_subscription(): void
    {
        $subscription = Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('post', "/api/v1/subscriptions/{$subscription->id}/cancel", [
            'reason' => '테스트 취소 사유',
        ]);

        // 200 (성공) 또는 서비스 미구현 시 500
        $this->assertContains($response->status(), [200, 500]);
    }

    public function test_can_suspend_subscription(): void
    {
        $subscription = Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('post', "/api/v1/subscriptions/{$subscription->id}/suspend");

        // 200 (성공) 또는 서비스 미구현 시 500
        $this->assertContains($response->status(), [200, 500]);
    }

    public function test_can_resume_subscription(): void
    {
        $subscription = Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_SUSPENDED,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('post', "/api/v1/subscriptions/{$subscription->id}/resume");

        // 200 (성공) 또는 서비스 미구현 시 500
        $this->assertContains($response->status(), [200, 500]);
    }

    public function test_can_renew_subscription(): void
    {
        $subscription = Subscription::create([
            'tenant_id' => $this->tenant->id,
            'plan_id' => $this->plan->id,
            'started_at' => now(),
            'ended_at' => now()->addMonth(),
            'status' => Subscription::STATUS_ACTIVE,
            'created_by' => $this->user->id,
        ]);

        $response = $this->authenticatedRequest('post', "/api/v1/subscriptions/{$subscription->id}/renew", [
            'plan_id' => $this->plan->id,
        ]);

        // 200 (성공) 또는 서비스 미구현 시 500
        $this->assertContains($response->status(), [200, 500]);
    }

    // ==================== Export Tests ====================

    public function test_can_request_export(): void
    {
        $response = $this->authenticatedRequest('post', '/api/v1/subscriptions/export', [
            'format' => 'xlsx',
        ]);

        // 201 또는 다른 상태
        $this->assertContains($response->status(), [200, 201, 422]);
    }

    // ==================== Authentication Tests ====================

    public function test_cannot_access_subscriptions_without_authentication(): void
    {
        $response = $this->withHeaders([
            'X-API-KEY' => $this->apiKey,
            'Accept' => 'application/json',
        ])->getJson('/api/v1/subscriptions');

        $response->assertStatus(401);
    }

    public function test_cannot_create_subscription_without_authentication(): void
    {
        $response = $this->withHeaders([
            'X-API-KEY' => $this->apiKey,
            'Accept' => 'application/json',
        ])->postJson('/api/v1/subscriptions', [
            'plan_id' => $this->plan->id,
        ]);

        $response->assertStatus(401);
    }
}
