<?php

namespace App\Tests\Entity;

use App\Entity\FormulesProductInclus;
use App\Entity\Product;
use App\Entity\ProductDoc;
use App\Entity\ProductPhotos;
use App\Entity\ProductReserve;
use App\Entity\ProductVideo;
use PHPUnit\Framework\TestCase;
use Cocur\Slugify\Slugify; // To test the slug() method

class ProductTest extends TestCase
{
    public function testGettersAndSetters()
    {
        $product = new Product();
        $now = new \DateTimeImmutable();

        $product->setRef('PROD-001');
        $product->setCategory('Category A');
        $product->setName('Test Product');
        $product->setPriceDay(10.0);
        $product->setPriceSup(5.0);
        $product->setCaution(20.0);
        $product->setImageName('product.jpg');
        $product->setImageSize(1024);
        $product->setUpdatedAt($now);
        $product->setProductId('ext_prod_id');
        $product->setDescription('Product description');
        $product->setQt(5);
        $product->setDimW(100.0);
        $product->setDimH(50.0);
        $product->setDimP(20.0);

        $this->assertEquals('PROD-001', $product->getRef());
        $this->assertEquals('Category A', $product->getCategory());
        $this->assertEquals('Test Product', $product->getName());
        $this->assertEquals(10.0, $product->getPriceDay());
        $this->assertEquals(5.0, $product->getPriceSup());
        $this->assertEquals(20.0, $product->getCaution());
        $this->assertEquals('product.jpg', $product->getImageName());
        $this->assertEquals(1024, $product->getImageSize());
        $this->assertSame($now, $product->getUpdatedAt());
        $this->assertEquals('ext_prod_id', $product->getProductId());
        $this->assertEquals('Product description', $product->getDescription());
        $this->assertEquals(5, $product->getQt());
        $this->assertEquals(100.0, $product->getDimW());
        $this->assertEquals(50.0, $product->getDimH());
        $this->assertEquals(20.0, $product->getDimP());
    }

    public function testSlug()
    {
        $product = new Product();
        // $product->setId(1); // ID is auto-generated, cannot be set directly in unit test
        $product->setName('Awesome Product');

        $this->assertEquals('awesome-product', $product->slug());
    }

    public function testJson()
    {
        $product = new Product();
        // $product->setId(1); // ID is auto-generated
        $product->setRef('PROD-JSON');
        $product->setName('JSON Product');

        $expectedJson = json_encode([
            'id' => null, // ID is null for new entity
            'ref' => 'PROD-JSON',
            'name' => 'JSON Product',
        ]);

        $this->assertEquals($expectedJson, $product->json());
    }

    public function testProductReservesCollection()
    {
        $product = new Product();
        $reserve = new ProductReserve();

        $this->assertCount(0, $product->getProductReserves());
        $product->addProductReserf($reserve); // Typo: addProductReserf
        $this->assertCount(1, $product->getProductReserves());
        $this->assertSame($product, $reserve->getProduct());

        $product->removeProductReserf($reserve); // Typo: removeProductReserf
        $this->assertCount(0, $product->getProductReserves());
        $this->assertNull($reserve->getProduct());
    }

    public function testProductDocsCollection()
    {
        $product = new Product();
        $doc = new ProductDoc();

        $this->assertCount(0, $product->getProductDocs());
        $product->addProductDoc($doc);
        $this->assertCount(1, $product->getProductDocs());
        $this->assertSame($product, $doc->getProduct());

        $product->removeProductDoc($doc);
        $this->assertCount(0, $product->getProductDocs());
        $this->assertNull($doc->getProduct());
    }

    public function testFormulesProductInclusesCollection()
    {
        $product = new Product();
        $inclus = new FormulesProductInclus();

        $this->assertCount(0, $product->getFormulesProductIncluses());
        $product->addFormulesProductInclus($inclus);
        $this->assertCount(1, $product->getFormulesProductIncluses());
        $this->assertSame($product, $inclus->getPRODUCT());

        $product->removeFormulesProductInclus($inclus);
        $this->assertCount(0, $product->getFormulesProductIncluses());
        $this->assertNull($inclus->getPRODUCT());
    }

    public function testProductPhotosCollection()
    {
        $product = new Product();
        $photo = new ProductPhotos();

        $this->assertCount(0, $product->getProductPhotos());
        $product->addProductPhoto($photo);
        $this->assertCount(1, $product->getProductPhotos());
        $this->assertSame($product, $photo->getProduct());

        $product->removeProductPhoto($photo);
        $this->assertCount(0, $product->getProductPhotos());
        $this->assertNull($photo->getProduct());
    }

    public function testProductVideosCollection()
    {
        $product = new Product();
        $video = new ProductVideo();

        $this->assertCount(0, $product->getProductVideos());
        $product->addProductVideo($video);
        $this->assertCount(1, $product->getProductVideos());
        $this->assertSame($product, $video->getProduct());

        $product->removeProductVideo($video);
        $this->assertCount(0, $product->getProductVideos());
        $this->assertNull($video->getProduct());
    }
}
