<?php

use App\Enums\IsActive;
use App\Enums\IssueSentiment;
use App\Enums\MediaType;
use App\Models\Classification;
use App\Models\Company;
use App\Models\IssueManagement;
use App\Models\Journalist;
use App\Models\MediaMonitoring;
use App\Models\PartnerMedia;
use App\Models\Theme;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

describe('Integration Tests', function () {
    it('can create complete media monitoring workflow', function () {
        // Create theme
        $theme = Theme::factory()->create(['name' => 'Politik']);

        // Create media monitoring
        $mediaMonitoring = MediaMonitoring::factory()->create([
            'title' => 'Berita Politik Terkini',
        ]);

        // Attach theme to media monitoring
        $mediaMonitoring->themes()->attach($theme->id);

        // Verify relationships
        expect($mediaMonitoring->themes)->toHaveCount(1)
            ->and($mediaMonitoring->themes->first()->name)->toBe('Politik');
    });

    it('can create complete journalist workflow', function () {
        // Create company
        $company = Company::factory()->create(['name' => 'Media Indonesia']);

        // Create partner media
        $partnerMedia = PartnerMedia::factory()->create([
            'company_id' => $company->id,
            'name' => 'Kompas.com',
            'type' => MediaType::ONLINE,
        ]);

        // Create journalist
        $journalist = Journalist::factory()->create([
            'partner_media_id' => $partnerMedia->id,
            'name' => 'John Doe',
        ]);

        // Verify relationships
        expect($journalist->partnerMedia->company->name)->toBe('Media Indonesia')
            ->and($journalist->partnerMedia->type)->toBe(MediaType::ONLINE);
    });

    it('can create issue management with sentiment', function () {
        // Create classification
        $classification = Classification::factory()->create(['name' => 'Infrastruktur']);

        // Create media monitoring
        $mediaMonitoring = MediaMonitoring::factory()->create();

        // Create issue management
        $issue = IssueManagement::factory()->create([
            'classification_id' => $classification->id,
            'media_monitoring_id' => $mediaMonitoring->id,
            'issue' => IssueSentiment::POSITIVE,
            'response' => IssueSentiment::POSITIVE,
        ]);

        // Verify data
        expect($issue->classification->name)->toBe('Infrastruktur')
            ->and($issue->issue)->toBe(IssueSentiment::POSITIVE)
            ->and($issue->response)->toBe(IssueSentiment::POSITIVE);
    });

    it('can filter active records', function () {
        // Create active and inactive themes
        Theme::factory()->create(['is_active' => IsActive::ACTIVE]);
        Theme::factory()->create(['is_active' => IsActive::INACTIVE]);

        $activeThemes = Theme::active()->get();

        expect($activeThemes)->toHaveCount(1)
            ->and($activeThemes->first()->is_active)->toBe(IsActive::ACTIVE);
    });

    it('verifies enum casting works correctly', function () {
        $partnerMedia = PartnerMedia::factory()->create([
            'type' => MediaType::TELEVISION,
        ]);

        expect($partnerMedia->type)->toBeInstanceOf(MediaType::class)
            ->and($partnerMedia->type)->toBe(MediaType::TELEVISION)
            ->and($partnerMedia->type->getLabel())->toBe('Televisi');
    });
});
