<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Services;

use App\Application\LiveComponents\ValueObjects\Activity;
use App\Application\LiveComponents\ValueObjects\ActivityFilter;
use App\Application\LiveComponents\ValueObjects\ActivityType;
use App\Framework\LiveComponents\Attributes\DataProvider;

/**
 * DemoActivityDataProvider - Demo implementation with sample data
 */
#[DataProvider(name: 'demo')]
final readonly class DemoActivityDataProvider implements ActivityDataProvider
{
    private array $activities;

    public function __construct()
    {
        $this->activities = $this->generateSampleActivities();
    }

    public function getActivities(ActivityFilter $filter = ActivityFilter::ALL, int $limit = 50): array
    {
        $filtered = array_filter(
            $this->activities,
            fn (Activity $activity) => $filter->matches($activity)
        );

        return array_slice($filtered, 0, $limit);
    }

    public function addActivity(Activity $activity): bool
    {
        // In demo mode, we just return true
        return true;
    }

    public function markAsRead(string $activityId): bool
    {
        // In demo mode, we just return true
        return true;
    }

    public function markAllAsRead(): int
    {
        // In demo mode, count unread activities
        return count(array_filter($this->activities, fn (Activity $a) => ! $a->isRead));
    }

    public function getUnreadCount(): int
    {
        return count(array_filter($this->activities, fn (Activity $a) => ! $a->isRead));
    }

    public function deleteActivity(string $activityId): bool
    {
        // In demo mode, we just return true
        return true;
    }

    /**
     * Generate sample activities for demo
     *
     * @return array<Activity>
     */
    private function generateSampleActivities(): array
    {
        $now = time();

        return [
            new Activity(
                id: 'act_1',
                type: ActivityType::COMMENT,
                userId: 'user_1',
                userName: 'Max Mustermann',
                content: 'Hat deinen Beitrag kommentiert: "Sehr interessant!"',
                timestamp: $now - 300, // 5 minutes ago
                isRead: false,
                avatarUrl: '/assets/avatars/user1.jpg'
            ),
            new Activity(
                id: 'act_2',
                type: ActivityType::LIKE,
                userId: 'user_2',
                userName: 'Anna Schmidt',
                content: 'Hat deinen Beitrag geliked',
                timestamp: $now - 1800, // 30 minutes ago
                isRead: false,
                avatarUrl: '/assets/avatars/user2.jpg'
            ),
            new Activity(
                id: 'act_3',
                type: ActivityType::SHARE,
                userId: 'user_3',
                userName: 'Peter Klein',
                content: 'Hat deinen Beitrag geteilt',
                timestamp: $now - 3600, // 1 hour ago
                isRead: true,
                avatarUrl: '/assets/avatars/user3.jpg'
            ),
            new Activity(
                id: 'act_4',
                type: ActivityType::FOLLOW,
                userId: 'user_4',
                userName: 'Lisa Groß',
                content: 'Folgt dir jetzt',
                timestamp: $now - 7200, // 2 hours ago
                isRead: true,
                avatarUrl: '/assets/avatars/user4.jpg'
            ),
            new Activity(
                id: 'act_5',
                type: ActivityType::MENTION,
                userId: 'user_5',
                userName: 'Tom Weber',
                content: 'Hat dich in einem Beitrag erwähnt',
                timestamp: $now - 14400, // 4 hours ago
                isRead: false,
                avatarUrl: '/assets/avatars/user5.jpg'
            ),
            new Activity(
                id: 'act_6',
                type: ActivityType::COMMENT,
                userId: 'user_6',
                userName: 'Sarah Lang',
                content: 'Hat auf deinen Kommentar geantwortet',
                timestamp: $now - 86400, // Yesterday
                isRead: true,
                avatarUrl: '/assets/avatars/user6.jpg'
            ),
            new Activity(
                id: 'act_7',
                type: ActivityType::LIKE,
                userId: 'user_7',
                userName: 'Michael Jung',
                content: 'Hat deinen Kommentar geliked',
                timestamp: $now - 86400 - 3600, // Yesterday
                isRead: true,
                avatarUrl: '/assets/avatars/user7.jpg'
            ),
            new Activity(
                id: 'act_8',
                type: ActivityType::SYSTEM,
                userId: 'system',
                userName: 'System',
                content: 'Dein Profil wurde erfolgreich aktualisiert',
                timestamp: $now - 172800, // 2 days ago
                isRead: true,
                avatarUrl: null
            ),
            new Activity(
                id: 'act_9',
                type: ActivityType::SHARE,
                userId: 'user_8',
                userName: 'Julia Becker',
                content: 'Hat deinen Beitrag in ihrer Story geteilt',
                timestamp: $now - 259200, // 3 days ago
                isRead: true,
                avatarUrl: '/assets/avatars/user8.jpg'
            ),
            new Activity(
                id: 'act_10',
                type: ActivityType::FOLLOW,
                userId: 'user_9',
                userName: 'Daniel Richter',
                content: 'Und 3 weitere Personen folgen dir jetzt',
                timestamp: $now - 345600, // 4 days ago
                isRead: true,
                avatarUrl: '/assets/avatars/user9.jpg',
                metadata: ['additional_followers' => 3]
            ),
        ];
    }
}
