<?php

use App\Models\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

describe('Application Model', function () {
    
    it('can create an application with factory', function () {
        $app = Application::factory()->create([
            'name' => 'Test App',
            'client_id' => 'test-client-id',
            'client_secret' => 'test-secret',
            'redirect_uri' => 'https://example.com/callback',
            'icon' => 'https://example.com/icon.png'
        ]);

        expect($app->name)->toBe('Test App');
        expect($app->client_id)->toBe('test-client-id');
        expect($app->client_secret)->toBe('test-secret');
        expect($app->redirect_uri)->toBe('https://example.com/callback');
        expect($app->icon)->toBe('https://example.com/icon.png');
    });

    it('guards only id field', function () {
        $app = new Application();
        
        // Test that id is guarded
        expect($app->getGuarded())->toBe(['id']);
        
        // Test that other fields can be mass assigned
        $app->fill([
            'name' => 'Mass Assigned App',
            'client_id' => 'mass-client-id',
            'client_secret' => 'mass-secret',
            'redirect_uri' => 'https://example.com/mass-callback',
            'icon' => 'https://example.com/mass-icon.png'
        ]);

        expect($app->name)->toBe('Mass Assigned App');
        expect($app->client_id)->toBe('mass-client-id');
    });

    it('returns icon URL through getIconUrl method', function () {
        $app = Application::factory()->create([
            'icon' => 'https://example.com/custom-icon.png'
        ]);

        expect($app->getIconUrl())->toBe('https://example.com/custom-icon.png');
    });

    it('handles null icon gracefully', function () {
        $app = Application::factory()->create(['icon' => null]);

        expect($app->getIconUrl())->toBe(null);
    });

    it('uses HasFactory trait', function () {
        expect(method_exists(Application::class, 'factory'))->toBe(true);
        expect(in_array('Illuminate\Database\Eloquent\Factories\HasFactory', class_uses(Application::class)))->toBe(true);
    });

    it('has proper database table name', function () {
        $app = new Application();
        expect($app->getTable())->toBe('applications');
    });

    it('has timestamps', function () {
        $app = Application::factory()->create();
        
        expect($app->created_at)->not()->toBeNull();
        expect($app->updated_at)->not()->toBeNull();
        expect($app->created_at)->toBeInstanceOf(\Illuminate\Support\Carbon::class);
        expect($app->updated_at)->toBeInstanceOf(\Illuminate\Support\Carbon::class);
    });

    it('generates unique client_id through factory', function () {
        $app1 = Application::factory()->create();
        $app2 = Application::factory()->create();
        
        expect($app1->client_id)->not()->toBe($app2->client_id);
    });

    it('can be updated', function () {
        $app = Application::factory()->create(['name' => 'Original Name']);
        
        $app->update(['name' => 'Updated Name']);
        
        expect($app->fresh()->name)->toBe('Updated Name');
    });

    it('can be deleted', function () {
        $app = Application::factory()->create();
        $id = $app->id;
        
        $app->delete();
        
        expect(Application::find($id))->toBeNull();
    });
});