<?php

declare(strict_types=1);

namespace App\Services\Sitemaps\Builders;

use App\Models\Collection;
use App\Services\Sitemaps\SitemapUrl;
use App\Services\Sitemaps\SitemapUrlBuilder;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

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

    public function name(): string
    {
        return 'collections';
    }

    protected function shardConfigKey(): string
    {
        return 'collections';
    }

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

    protected function query(): Builder
    {
        return Collection::query()
            ->with('user:id,username')
            ->public()
            ->whereNull('canonical_collection_id')
            ->orderBy('id');
    }

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