<?php

declare(strict_types=1);

use App\Domain\PreSave\ValueObjects\StreamingPlatform;
use App\Domain\PreSave\ValueObjects\TrackUrl;

describe('TrackUrl', function () {
    describe('construction validation', function () {
        it('creates valid TrackUrl with all parameters', function () {
            $trackUrl = new TrackUrl(
                platform: StreamingPlatform::SPOTIFY,
                url: 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp',
                trackId: '3n3Ppam7vgaVa1iaRUc9Lp'
            );

            expect(strpos(var_export($trackUrl->platform, true), "SPOTIFY") !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

//         it('throws exception for empty URL', function () {
//             expect(fn() => new TrackUrl(
//                 platform: StreamingPlatform::SPOTIFY,
//                 url: '',
//                 trackId: 'test123'
//             ))->toThrow(\InvalidArgumentException::class, 'Track URL cannot be empty');
//         });

//         it('throws exception for empty track ID', function () {
//             expect(fn() => new TrackUrl(
//                 platform: StreamingPlatform::SPOTIFY,
//                 url: 'https://open.spotify.com/track/test',
//                 trackId: ''
//             ))->toThrow(\InvalidArgumentException::class, 'Track ID cannot be empty');
//         });
    });

    describe('Spotify URL parsing', function () {
        it('extracts track ID from Spotify URL', function () {
            $trackUrl = TrackUrl::fromUrl('https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp');

            expect(strpos(var_export($trackUrl->platform, true), "SPOTIFY") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

        it('handles Spotify URL with query parameters', function () {
            $trackUrl = TrackUrl::fromUrl('https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp?si=abc123');

            expect(strpos(var_export($trackUrl->platform, true), "SPOTIFY") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

//         it('throws exception for invalid Spotify URL', function () {
//             expect(fn() => TrackUrl::fromUrl('https://open.spotify.com/playlist/123'))->toThrow(\InvalidArgumentException::class, 'Invalid Spotify track URL');
//         });
    });

    describe('Tidal URL parsing', function () {
        it('extracts track ID from Tidal URL', function () {
            $trackUrl = TrackUrl::fromUrl('https://tidal.com/track/12345678');

            expect(strpos(var_export($trackUrl->platform, true), "TIDAL") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '12345678') !== false)->toBeTrue();
        });

//         it('throws exception for invalid Tidal URL', function () {
//             expect(fn() => TrackUrl::fromUrl('https://tidal.com/album/123'))->toThrow(\InvalidArgumentException::class, 'Invalid Tidal track URL');
//         });
    });

    describe('Apple Music URL parsing', function () {
        it('extracts track ID from Apple Music URL', function () {
            $trackUrl = TrackUrl::fromUrl('https://music.apple.com/us/album/song-name/id1234567890');

            expect(strpos(var_export($trackUrl->platform, true), "APPLE_MUSIC") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '1234567890') !== false)->toBeTrue();
        });

//         it('throws exception for invalid Apple Music URL', function () {
//             expect(fn() => TrackUrl::fromUrl('https://music.apple.com/us/album/test'))->toThrow(\InvalidArgumentException::class, 'Invalid Apple Music track URL');
//         });
    });

    describe('unsupported platform detection', function () {
//         it('throws exception for empty URL', function () {
//             expect(fn() => TrackUrl::fromUrl(''))->toThrow(\InvalidArgumentException::class, 'URL cannot be empty');
//         });
// 
//         it('throws exception for unsupported platform', function () {
//             expect(fn() => TrackUrl::fromUrl('https://soundcloud.com/artist/track'))->toThrow(\InvalidArgumentException::class, 'Unsupported streaming platform URL');
//         });
    });

    describe('platform-specific creation', function () {
        it('creates Spotify TrackUrl with ID', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::SPOTIFY, '3n3Ppam7vgaVa1iaRUc9Lp');

            expect(strpos(var_export($trackUrl->platform, true), "SPOTIFY") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

        it('creates Tidal TrackUrl with ID', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::TIDAL, '12345678');

            expect(strpos(var_export($trackUrl->platform, true), "TIDAL") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '12345678') !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://tidal.com/track/12345678') !== false)->toBeTrue();
        });

        it('creates Apple Music TrackUrl with ID', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::APPLE_MUSIC, '1234567890');

            expect(strpos(var_export($trackUrl->platform, true), "APPLE_MUSIC") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '1234567890') !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://music.apple.com/track/id1234567890') !== false)->toBeTrue();
        });

        it('creates Deezer TrackUrl with ID', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::DEEZER, '123456');

            expect(strpos(var_export($trackUrl->platform, true), "DEEZER") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '123456') !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://www.deezer.com/track/123456') !== false)->toBeTrue();
        });

        it('creates YouTube Music TrackUrl with ID', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::YOUTUBE_MUSIC, 'dQw4w9WgXcQ');

            expect(strpos(var_export($trackUrl->platform, true), "YOUTUBE_MUSIC") !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, 'dQw4w9WgXcQ') !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://music.youtube.com/watch?v=dQw4w9WgXcQ') !== false)->toBeTrue();
        });
    });

    describe('shareable URL', function () {
        it('returns original URL as shareable URL', function () {
            $originalUrl = 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp';
            $trackUrl = TrackUrl::fromUrl($originalUrl);

            expect(strpos($trackUrl->getShareableUrl(), $originalUrl) !== false)->toBeTrue();
        });
    });

    describe('array conversion', function () {
        it('converts to array with all properties', function () {
            $trackUrl = TrackUrl::create(StreamingPlatform::SPOTIFY, '3n3Ppam7vgaVa1iaRUc9Lp');
            $array = $trackUrl->toArray();

            expect(strpos($array['platform'], 'spotify') !== false)->toBeTrue();
            expect(strpos($array['url'], 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
            expect(strpos($array['track_id'], '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

        it('creates from array', function () {
            $data = [
                'platform' => 'spotify',
                'url' => 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp',
                'track_id' => '3n3Ppam7vgaVa1iaRUc9Lp',
            ];

            $trackUrl = TrackUrl::fromArray($data);

            expect(strpos(var_export($trackUrl->platform, true), "SPOTIFY") !== false)->toBeTrue();
            expect(strpos($trackUrl->url, 'https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
            expect(strpos($trackUrl->trackId, '3n3Ppam7vgaVa1iaRUc9Lp') !== false)->toBeTrue();
        });

        it('round-trips through array conversion', function () {
            $original = TrackUrl::create(StreamingPlatform::TIDAL, '12345678');
            $array = $original->toArray();
            $restored = TrackUrl::fromArray($array);

            expect($restored->platform === $original->platform)->toBeTrue();
            expect($restored->url === $original->url)->toBeTrue();
            expect($restored->trackId === $original->trackId)->toBeTrue();
        });
    });
});
