<?php

use Database\Seeders\CmsContentSeeder;
use Database\Seeders\CmsFaqSeeder;
use Database\Seeders\CmsIndustrySeeder;
use Database\Seeders\CmsLinkedinPostSeeder;
use Database\Seeders\CmsNewsItemSeeder;
use FluxCms\Core\Models\CmsContent;
use FluxCms\Core\Models\CmsFaq;
use FluxCms\Core\Models\CmsIndustry;
use FluxCms\Core\Models\CmsLinkedinPost;
use FluxCms\Core\Models\CmsNewsItem;

it('seeds cms content from lang files', function () {
    $this->seed(CmsContentSeeder::class);

    expect(CmsContent::count())->toBeGreaterThan(0);

    $welcomeContent = CmsContent::forGroup('welcome')->get();
    expect($welcomeContent->count())->toBeGreaterThan(0);

    $heroHeading = CmsContent::where('group', 'welcome')
        ->where('key', 'hero.heading_main')
        ->first();
    expect($heroHeading)->not->toBeNull()
        ->and($heroHeading->getTranslation('value', 'de'))->not->toBeEmpty();
});

it('seeds news items from lang files', function () {
    $this->seed(CmsNewsItemSeeder::class);

    expect(CmsNewsItem::count())->toBeGreaterThan(0);

    $first = CmsNewsItem::ordered()->first();
    expect($first->getTranslation('title', 'de'))->not->toBeEmpty();
});

it('seeds industries from lang files', function () {
    $this->seed(CmsIndustrySeeder::class);

    expect(CmsIndustry::count())->toBeGreaterThan(0);

    $first = CmsIndustry::ordered()->first();
    expect($first->getTranslation('name', 'de'))->toBe('FMCG');
});

it('seeds faqs from lang files', function () {
    $this->seed(CmsFaqSeeder::class);

    expect(CmsFaq::count())->toBeGreaterThan(0);

    $categories = CmsFaq::distinct()->pluck('category');
    expect($categories)->toContain('general');
});

it('seeds linkedin posts', function () {
    $this->seed(CmsLinkedinPostSeeder::class);

    expect(CmsLinkedinPost::count())->toBe(3);

    $first = CmsLinkedinPost::ordered()->first();
    expect($first->source)->toBe('manual')
        ->and($first->getTranslation('title', 'de'))->not->toBeEmpty();
});

it('does not create duplicates when run twice', function () {
    $this->seed(CmsIndustrySeeder::class);
    $countAfterFirst = CmsIndustry::count();

    $this->seed(CmsIndustrySeeder::class);
    $countAfterSecond = CmsIndustry::count();

    expect($countAfterSecond)->toBe($countAfterFirst);
});
