<?php

use FluxCms\Core\Models\CmsDownload;
use FluxCms\Core\Models\CmsFaq;
use FluxCms\Core\Models\CmsIndustry;
use FluxCms\Core\Models\CmsLinkedinPost;
use FluxCms\Core\Models\CmsNewsItem;

it('creates a cms faq with translations', function () {
    $faq = CmsFaq::create([
        'category' => 'general',
        'question' => ['de' => 'Was ist das?', 'en' => 'What is this?'],
        'answer' => ['de' => 'Eine Antwort', 'en' => 'An answer'],
        'is_published' => true,
        'order' => 0,
    ]);

    expect($faq->getTranslation('question', 'de'))->toBe('Was ist das?')
        ->and($faq->getTranslation('question', 'en'))->toBe('What is this?');
});

it('scopes faqs by category', function () {
    CmsFaq::create([
        'category' => 'general',
        'question' => ['de' => 'Frage 1'],
        'answer' => ['de' => 'Antwort 1'],
        'order' => 0,
    ]);
    CmsFaq::create([
        'category' => 'technical',
        'question' => ['de' => 'Frage 2'],
        'answer' => ['de' => 'Antwort 2'],
        'order' => 0,
    ]);

    expect(CmsFaq::byCategory('general')->count())->toBe(1)
        ->and(CmsFaq::byCategory('technical')->count())->toBe(1);
});

it('scopes published items', function () {
    CmsFaq::create([
        'category' => 'test',
        'question' => ['de' => 'Sichtbar'],
        'answer' => ['de' => 'Ja'],
        'is_published' => true,
        'order' => 0,
    ]);
    CmsFaq::create([
        'category' => 'test',
        'question' => ['de' => 'Versteckt'],
        'answer' => ['de' => 'Nein'],
        'is_published' => false,
        'order' => 1,
    ]);

    expect(CmsFaq::published()->count())->toBe(1);
});

it('creates a news item with all fields', function () {
    $item = CmsNewsItem::create([
        'icon' => 'document-check',
        'text' => ['de' => 'Capability: Test', 'en' => 'Capability: Test'],
        'title' => ['de' => 'Titel DE', 'en' => 'Title EN'],
        'excerpt' => ['de' => 'Kurztext', 'en' => 'Short text'],
        'content' => ['de' => '<p>Inhalt</p>', 'en' => '<p>Content</p>'],
        'image' => '/images/test.jpg',
        'date' => '2026-01-01',
        'author' => 'Test Author',
        'pdf_path' => '/pdfs/test.pdf',
        'pdf_open_text' => ['de' => 'Öffnen', 'en' => 'Open'],
        'is_published' => true,
        'order' => 0,
    ]);

    expect($item->toFrontendArray('de'))
        ->toHaveKey('icon', 'document-check')
        ->toHaveKey('title', 'Titel DE')
        ->toHaveKey('pdf_path', '/pdfs/test.pdf');
});

it('creates and queries industries', function () {
    CmsIndustry::create(['name' => ['de' => 'FMCG', 'en' => 'FMCG'], 'order' => 0, 'is_published' => true]);
    CmsIndustry::create(['name' => ['de' => 'Beauty', 'en' => 'Beauty'], 'order' => 1, 'is_published' => true]);
    CmsIndustry::create(['name' => ['de' => 'Entwurf', 'en' => 'Draft'], 'order' => 2, 'is_published' => false]);

    expect(CmsIndustry::published()->count())->toBe(2)
        ->and(CmsIndustry::ordered()->first()->getTranslation('name', 'de'))->toBe('FMCG');
});

it('creates a download with category', function () {
    $dl = CmsDownload::create([
        'title' => ['de' => 'Case Study', 'en' => 'Case Study'],
        'description' => ['de' => 'Beschreibung', 'en' => 'Description'],
        'category' => 'case_study',
        'file_path' => '/pdfs/case-study.pdf',
        'is_published' => true,
        'order' => 0,
    ]);

    expect(CmsDownload::byCategory('case_study')->count())->toBe(1)
        ->and($dl->getTranslation('title', 'de'))->toBe('Case Study');
});

it('creates a linkedin post with source flag', function () {
    CmsLinkedinPost::create([
        'title' => ['de' => 'Post Titel'],
        'content' => ['de' => '<p>Inhalt</p>'],
        'author' => 'Test',
        'date' => '2026-01-01',
        'source' => 'manual',
        'is_published' => true,
        'order' => 0,
    ]);
    CmsLinkedinPost::create([
        'title' => ['de' => 'API Post'],
        'content' => ['de' => '<p>API</p>'],
        'linkedin_id' => 'ext-123',
        'source' => 'api',
        'is_published' => true,
        'order' => 1,
    ]);

    expect(CmsLinkedinPost::manual()->count())->toBe(1)
        ->and(CmsLinkedinPost::fromApi()->count())->toBe(1);
});
