<?php

declare(strict_types=1);

use Builder\Seo\Casts\SeoDataCast;
use Illuminate\Database\Eloquent\Model;

function makeCast(): SeoDataCast
{
    return new SeoDataCast;
}

function makeModel(): Model
{
    return new class extends Model {};
}

it('get maps seo_title, seo_description and seo_robots to composite object', function (): void {
    $result = makeCast()->get(makeModel(), 'seo', null, [
        'seo_title' => 'My Title',
        'seo_description' => 'My description.',
        'seo_robots' => 'noindex',
    ]);

    expect($result)->toBe([
        'title' => 'My Title',
        'description' => 'My description.',
        'robots' => 'noindex',
    ]);
});

it('get returns index as default robots when seo_robots column is null', function (): void {
    $result = makeCast()->get(makeModel(), 'seo', null, [
        'seo_title' => 'Title',
        'seo_description' => null,
        'seo_robots' => null,
    ]);

    expect($result['robots'])->toBe('index');
});

it('get returns null for title and description when columns are absent', function (): void {
    $result = makeCast()->get(makeModel(), 'seo', null, []);

    expect($result['title'])->toBeNull()
        ->and($result['description'])->toBeNull()
        ->and($result['robots'])->toBe('index');
});

it('set maps composite array to three individual column keys', function (): void {
    $result = makeCast()->set(makeModel(), 'seo', [
        'title' => 'Page Title',
        'description' => 'Meta description here.',
        'robots' => 'nofollow',
    ], []);

    expect($result)->toBe([
        'seo_title' => 'Page Title',
        'seo_description' => 'Meta description here.',
        'seo_robots' => 'nofollow',
    ]);
});

it('set defaults robots to index when not provided', function (): void {
    $result = makeCast()->set(makeModel(), 'seo', [
        'title' => 'Title',
        'description' => null,
    ], []);

    expect($result['seo_robots'])->toBe('index');
});

it('set maps null value to three null columns', function (): void {
    $result = makeCast()->set(makeModel(), 'seo', null, []);

    expect($result)->toBe([
        'seo_title' => null,
        'seo_description' => null,
        'seo_robots' => null,
    ]);
});
