<?php

declare(strict_types=1);

namespace App\Application\Campaign;

use App\Application\Campaign\ValueObjects\Campaign;

/**
 * In-Memory Campaign Repository (for testing)
 *
 * Temporary implementation until real database repository is created
 */
final readonly class InMemoryCampaignRepository implements CampaignRepository
{
    private array $campaigns;

    public function __construct()
    {
        // Sample campaign data
        $this->campaigns = [
            Campaign::fromArray([
                'id' => '1',
                'slug' => 'test-campaign',
                'artist_name' => 'Test Artist',
                'album_title' => 'Amazing Album',
                'description' => 'This is a test campaign for pre-save functionality',
                'artwork_url' => 'https://via.placeholder.com/400',
                'release_date' => '2024-12-15',
                'total_saves' => 1250,
                'track_count' => 3,
                'spotify_enabled' => true,
                'apple_music_enabled' => true,
                'spotify_uri' => 'spotify:album:test123',
                'apple_music_id' => 'apple456',
                'tracks' => [
                    [
                        'id' => 'track_1',
                        'position' => 1,
                        'title' => 'Opening Track',
                        'duration' => 240,
                        'preview_url' => null,
                        'spotify_id' => 'spotify_track_1',
                        'apple_music_id' => 'apple_track_1',
                    ],
                    [
                        'id' => 'track_2',
                        'position' => 2,
                        'title' => 'Middle Track',
                        'duration' => 210,
                        'preview_url' => null,
                        'spotify_id' => 'spotify_track_2',
                        'apple_music_id' => 'apple_track_2',
                    ],
                    [
                        'id' => 'track_3',
                        'position' => 3,
                        'title' => 'Closing Track',
                        'duration' => 180,
                        'preview_url' => null,
                        'spotify_id' => 'spotify_track_3',
                        'apple_music_id' => 'apple_track_3',
                    ],
                ],
                'status' => 'active',
            ]),
        ];
    }

    public function findBySlug(string $slug): ?Campaign
    {
        foreach ($this->campaigns as $campaign) {
            if ($campaign->slug === $slug) {
                return $campaign;
            }
        }

        return null;
    }

    public function findById(string $id): ?Campaign
    {
        foreach ($this->campaigns as $campaign) {
            if ($campaign->id === $id) {
                return $campaign;
            }
        }

        return null;
    }

    public function hasUserSaved(string $campaignId, string $sessionId): bool
    {
        // Mock: always return false for testing
        return false;
    }

    public function recordPreSave(
        string $campaignId,
        string $platform,
        string $sessionId,
        ?string $email = null
    ): void {
        // Mock: do nothing for now
    }

    public function incrementSaveCount(string $campaignId): void
    {
        // Mock: do nothing for now
    }

    public function recordEmailSubscription(
        string $campaignId,
        string $email,
        ?string $name = null,
        bool $newsletter = false
    ): void {
        // Mock: do nothing for now
    }

    public function findAll(): array
    {
        return $this->campaigns;
    }

    public function create(Campaign $campaign): Campaign
    {
        // Mock: return same campaign
        return $campaign;
    }

    public function update(Campaign $campaign): void
    {
        // Mock: do nothing
    }

    public function delete(string $id): void
    {
        // Mock: do nothing
    }
}
