<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Services;

use App\Application\LiveComponents\ValueObjects\ScrollItem;
use App\Framework\LiveComponents\Attributes\DataProvider;

/**
 * DemoScrollDataProvider - Demo implementation with generated scroll items
 */
#[DataProvider(name: 'demo')]
final readonly class DemoScrollDataProvider implements ScrollDataProvider
{
    private const TOTAL_ITEMS = 100;

    public function getItems(int $page, int $pageSize): array
    {
        $allItems = $this->generateAllItems();
        $offset = ($page - 1) * $pageSize;

        if ($offset >= count($allItems)) {
            return [];
        }

        $items = array_slice($allItems, $offset, $pageSize);

        return array_map(function (array $item) {
            return new ScrollItem(
                id: $item['id'],
                title: $item['title'],
                description: $item['description'],
                author: $item['author'],
                date: $item['date'],
                category: $item['category'],
                image: $item['image'],
                likes: $item['likes'],
                views: $item['views']
            );
        }, $items);
    }

    public function getTotalCount(): int
    {
        return self::TOTAL_ITEMS;
    }

    public function hasMore(int $currentPage, int $pageSize): bool
    {
        $offset = $currentPage * $pageSize;

        return $offset < $this->getTotalCount();
    }

    private function generateAllItems(): array
    {
        $items = [];

        for ($i = 1; $i <= self::TOTAL_ITEMS; $i++) {
            $items[] = [
                'id' => $i,
                'title' => $this->generateTitle($i),
                'description' => $this->generateDescription($i),
                'author' => $this->getRandomAuthor(),
                'date' => $this->getRandomDate(),
                'category' => $this->getRandomCategory(),
                'image' => "https://picsum.photos/seed/{$i}/400/300",
                'likes' => rand(0, 1000),
                'views' => rand(100, 10000),
            ];
        }

        return $items;
    }

    private function generateTitle(int $index): string
    {
        $titles = [
            'Understanding Modern Web Development',
            'The Future of Artificial Intelligence',
            'Best Practices for API Design',
            'Introduction to Machine Learning',
            'Building Scalable Applications',
            'Database Optimization Techniques',
            'Microservices Architecture Patterns',
            'Security in Cloud Computing',
            'Advanced JavaScript Concepts',
            'The Art of Code Review',
        ];

        $baseTitle = $titles[$index % count($titles)];

        return "{$baseTitle} - Part {$index}";
    }

    private function generateDescription(int $index): string
    {
        $descriptions = [
            'Explore the latest trends and best practices in software development.',
            'Learn how to build efficient and maintainable applications.',
            'Discover advanced techniques for solving complex problems.',
            'A comprehensive guide to modern development methodologies.',
            'Deep dive into architectural patterns and design principles.',
        ];

        return $descriptions[$index % count($descriptions)];
    }

    private function getRandomAuthor(): string
    {
        $authors = [
            'Sarah Johnson',
            'Michael Chen',
            'Emma Williams',
            'David Rodriguez',
            'Lisa Anderson',
            'James Martinez',
            'Anna Garcia',
            'Robert Taylor',
        ];

        return $authors[array_rand($authors)];
    }

    private function getRandomDate(): string
    {
        $daysAgo = rand(1, 365);
        $timestamp = time() - ($daysAgo * 86400);

        return date('Y-m-d', $timestamp);
    }

    private function getRandomCategory(): string
    {
        $categories = [
            'Technology',
            'Development',
            'Design',
            'Business',
            'Tutorial',
            'News',
            'Opinion',
            'Research',
        ];

        return $categories[array_rand($categories)];
    }
}
