<?php

namespace Tests\Unit\Services\Parsers;

use App\Contracts\ArticleParserInterface;
use App\Services\Parsers\GuardianArticleParser;
use Tests\TestCase;

class GuardianArticleParserTest extends TestCase
{
    private GuardianArticleParser $parser;

    protected function setUp(): void
    {
        parent::setUp();
        $this->parser = new GuardianArticleParser;
    }

    public function test_implements_article_parser_interface(): void
    {
        $this->assertInstanceOf(ArticleParserInterface::class, $this->parser);
    }

    public function test_can_parse_guardian_url(): void
    {
        $this->assertTrue($this->parser->canParse('https://www.theguardian.com/world/2026/mar/08/some-article'));
    }

    public function test_can_parse_guardian_url_without_www(): void
    {
        $this->assertTrue($this->parser->canParse('https://theguardian.com/world/2026/mar/08/some-article'));
    }

    public function test_cannot_parse_non_guardian_url(): void
    {
        $this->assertFalse($this->parser->canParse('https://www.vrt.be/vrtnws/en/article'));
        $this->assertFalse($this->parser->canParse('https://www.belganewsagency.eu/article'));
    }

    public function test_get_source_name(): void
    {
        $this->assertEquals('The Guardian', $this->parser->getSourceName());
    }

    public function test_extract_data_delegates_to_page_parser(): void
    {
        $html = '
            <html>
                <head>
                    <meta property="og:title" content="Test Title"/>
                    <meta property="og:description" content="Test Description"/>
                </head>
                <body><p>Content</p></body>
            </html>
        ';

        $data = $this->parser->extractData($html);

        $this->assertIsArray($data);
        $this->assertArrayHasKey('title', $data);
        $this->assertEquals('Test Title', $data['title']);
    }
}
