<?php

declare(strict_types=1);

namespace App\Services;

use App\Models\ArtworkComment;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

final class ReceivedCommentsInboxService
{
    public function queryForUser(User $user): Builder
    {
        return ArtworkComment::query()
            ->whereHas('artwork', function ($query) use ($user): void {
                $query->where('user_id', $user->id)
                    ->where('is_approved', true)
                    ->whereNull('deleted_at');
            })
            ->where('user_id', '!=', $user->id)
            ->where('is_approved', true)
            ->whereNull('deleted_at');
    }

    public function unreadCountForUser(User $user): int
    {
        return (int) $this->unreadQueryForUser($user)->count();
    }

    public function markInboxRead(User $user): void
    {
        $readAt = Carbon::now();

        $this->unreadQueryForUser($user)
            ->select('artwork_comments.id')
            ->orderBy('artwork_comments.id')
            ->chunkById(200, function ($comments) use ($user, $readAt): void {
                $rows = collect($comments)->map(function ($comment) use ($user, $readAt): array {
                    return [
                        'user_id' => $user->id,
                        'artwork_comment_id' => (int) $comment->id,
                        'read_at' => $readAt,
                        'created_at' => $readAt,
                        'updated_at' => $readAt,
                    ];
                })->all();

                if ($rows !== []) {
                    DB::table('user_received_comment_reads')->insertOrIgnore($rows);
                }
            }, 'artwork_comments.id', 'id');
    }

    private function unreadQueryForUser(User $user): Builder
    {
        return $this->queryForUser($user)
            ->whereNotExists(function ($query) use ($user): void {
                $query->selectRaw('1')
                    ->from('user_received_comment_reads as ucr')
                    ->whereColumn('ucr.artwork_comment_id', 'artwork_comments.id')
                    ->where('ucr.user_id', $user->id);
            });
    }
}
