<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\BasicBasketProduct;
use App\Models\Store;
use App\Models\StoreProductTarget;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SeederIdempotencyTest extends TestCase
{
    use RefreshDatabase;

    public function test_database_seeder_is_idempotent_for_products_stores_targets_and_user(): void
    {
        $this->seed(DatabaseSeeder::class);
        $this->seed(DatabaseSeeder::class);

        $this->assertSame(24, BasicBasketProduct::query()->count());
        $this->assertSame(3, Store::query()->count());
        $this->assertSame(72, StoreProductTarget::query()->count());
        $this->assertSame(72, StoreProductTarget::query()->distinct('slug')->count('slug'));
        $this->assertSame(1, User::query()->where('email', 'test@example.com')->count());
        $this->assertDatabaseHas('store_product_targets', [
            'slug' => 'tortilla-maiz-kilo-chedraui',
            'expected_seller' => 'Chedraui',
        ]);
    }

    public function test_new_shopping_fields_are_stored_correctly_for_bodega_aurrera_tortilla(): void
    {
        $this->seed(DatabaseSeeder::class);

        $target = StoreProductTarget::query()
            ->where('slug', 'tortilla-maiz-kilo-bodega-aurrera')
            ->firstOrFail();

        $this->assertSame('Bodega Aurrera Despensa a tu Casa', $target->expected_seller);
        $this->assertSame(2, $target->expected_position);
        $this->assertSame(2, $target->expected_rank_group);
        $this->assertSame('1712851062477582571', $target->expected_data_docid);
        $this->assertNull($target->expected_product_id);
        $this->assertNull($target->expected_gid);
        $this->assertIsArray($target->expected_shopping_url_contains);
        $this->assertContains('headlineOfferDocid:1712851062477582571', $target->expected_shopping_url_contains);
    }

    public function test_targets_without_new_fields_store_null_correctly(): void
    {
        $this->seed(DatabaseSeeder::class);

        // Chedraui tortilla no tiene override con los nuevos campos
        $target = StoreProductTarget::query()
            ->where('slug', 'tortilla-maiz-kilo-chedraui')
            ->firstOrFail();

        $this->assertNull($target->expected_rank_group);
        $this->assertNull($target->expected_data_docid);
        $this->assertNull($target->expected_product_id);
        $this->assertNull($target->expected_gid);
        $this->assertNull($target->expected_shopping_url_contains);
    }

    public function test_seeder_running_twice_does_not_change_new_fields(): void
    {
        $this->seed(DatabaseSeeder::class);

        $firstRun = StoreProductTarget::query()
            ->where('slug', 'tortilla-maiz-kilo-bodega-aurrera')
            ->value('expected_data_docid');

        $this->seed(DatabaseSeeder::class);

        $secondRun = StoreProductTarget::query()
            ->where('slug', 'tortilla-maiz-kilo-bodega-aurrera')
            ->value('expected_data_docid');

        $this->assertSame($firstRun, $secondRun);
    }
}
