<?php

declare(strict_types=1);

namespace App\Services;

use App\Models\Collection;
use App\Models\User;

class CollectionDashboardService
{
    public function __construct(
        private readonly CollectionCampaignService $campaigns,
        private readonly CollectionHealthService $health,
    ) {
    }

    public function build(User $user): array
    {
        $collections = Collection::query()
            ->with(['user:id,username,name', 'coverArtwork:id,user_id,title,slug,hash,thumb_ext,published_at,is_public,is_approved,deleted_at'])
            ->ownedBy((int) $user->id)
            ->orderByDesc('ranking_score')
            ->orderByDesc('updated_at')
            ->get();

        $pendingSubmissions = $collections->sum(fn (Collection $collection) => $collection->submissions()->where('status', Collection::SUBMISSION_PENDING)->count());

        return [
            'summary' => [
                'total' => $collections->count(),
                'drafts' => $collections->where('lifecycle_state', Collection::LIFECYCLE_DRAFT)->count(),
                'scheduled' => $collections->where('lifecycle_state', Collection::LIFECYCLE_SCHEDULED)->count(),
                'published' => $collections->whereIn('lifecycle_state', [Collection::LIFECYCLE_PUBLISHED, Collection::LIFECYCLE_FEATURED])->count(),
                'archived' => $collections->where('lifecycle_state', Collection::LIFECYCLE_ARCHIVED)->count(),
                'pending_submissions' => (int) $pendingSubmissions,
                'needs_review' => $collections->where('health_state', Collection::HEALTH_NEEDS_REVIEW)->count(),
                'duplicate_risk' => $collections->where('health_state', Collection::HEALTH_DUPLICATE_RISK)->count(),
                'placement_blocked' => $collections->where('placement_eligibility', false)->count(),
            ],
            'top_performing' => $collections->take(6),
            'needs_attention' => $collections->filter(function (Collection $collection): bool {
                return $collection->lifecycle_state === Collection::LIFECYCLE_DRAFT
                    || (int) $collection->quality_score < 45
                    || $collection->moderation_status !== Collection::MODERATION_ACTIVE
                    || ($collection->health_state !== null && $collection->health_state !== Collection::HEALTH_HEALTHY)
                    || ! $collection->isPlacementEligible();
            })->take(6)->values(),
            'expiring_campaigns' => $this->campaigns->expiringCampaignsForOwner($user),
            'health_warnings' => $collections
                ->filter(fn (Collection $collection): bool => $collection->health_state !== null && $collection->health_state !== Collection::HEALTH_HEALTHY)
                ->take(8)
                ->map(fn (Collection $collection): array => [
                    'collection_id' => (int) $collection->id,
                    'title' => (string) $collection->title,
                    'health' => $this->health->summary($collection),
                ])
                ->values()
                ->all(),
        ];
    }
}
