<?php

declare(strict_types=1);

namespace App\Services\Sitemaps\Builders;

use App\Services\Sitemaps\SitemapUrl;
use App\Services\Sitemaps\SitemapUrlBuilder;
use cPad\Plugins\Forum\Models\ForumTopic;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

final class ForumThreadsSitemapBuilder extends AbstractIdShardableSitemapBuilder
{
    public function __construct(private readonly SitemapUrlBuilder $urls)
    {
    }

    public function name(): string
    {
        return 'forum-threads';
    }

    protected function shardConfigKey(): string
    {
        return 'forum-threads';
    }

    protected function mapRecord(Model $record): ?SitemapUrl
    {
        return $this->urls->forumTopic($record);
    }

    protected function query(): Builder
    {
        return ForumTopic::query()
            ->visible()
            ->whereHas('board', fn ($query) => $query->active())
            ->orderBy('id');
    }

    protected function qualifiedIdColumn(): string
    {
        return 'forum_topics.id';
    }
}