<?php

namespace App\Http\Controllers;

use App\Models\Anime;
use App\Models\Genre;
use App\Models\Setting;
use App\Models\BlogPost;

class SitemapController extends Controller
{
    public function index()
    {
        $domain = rtrim(Setting::get('seo_canonical_domain', config('app.url')), '/');

        return response()
            ->view('sitemap_index', compact('domain'))
            ->header('Content-Type', 'application/xml; charset=utf-8');
    }

    public function main()
    {
        $domain = rtrim(Setting::get('seo_canonical_domain', config('app.url')), '/');

        $genres = Genre::where('is_active', true)->select('slug', 'updated_at')->get();

        $staticPages = [
            ['loc' => '/',             'priority' => '1.0', 'changefreq' => 'daily'],
            ['loc' => '/search',       'priority' => '0.8', 'changefreq' => 'daily'],
            ['loc' => '/anime-request','priority' => '0.5', 'changefreq' => 'weekly'],
            ['loc' => '/blog',         'priority' => '0.8', 'changefreq' => 'daily'],
        ];

        return response()
            ->view('sitemaps.main', compact('domain', 'genres', 'staticPages'))
            ->header('Content-Type', 'application/xml; charset=utf-8');
    }

    public function animes()
    {
        $domain = rtrim(Setting::get('seo_canonical_domain', config('app.url')), '/');

        $animes = Anime::where('is_published', true)
            ->select('slug', 'updated_at', 'rating', 'cover_image', 'title')
            ->orderByDesc('rating')
            ->get()
            ->each(function ($anime) use ($domain) {
                $anime->cover_image_url = $anime->cover_image
                    ? (str_starts_with($anime->cover_image, 'http') ? $anime->cover_image : $domain . '/storage/' . $anime->cover_image)
                    : null;
            });

        return response()
            ->view('sitemaps.animes', compact('domain', 'animes'))
            ->header('Content-Type', 'application/xml; charset=utf-8');
    }

    public function blog()
    {
        $domain = rtrim(Setting::get('seo_canonical_domain', config('app.url')), '/');

        $posts = BlogPost::published()
            ->select('slug', 'updated_at', 'published_at', 'cover_image', 'title', 'excerpt')
            ->orderByDesc('published_at')
            ->get();

        return response()
            ->view('sitemaps.blog', compact('domain', 'posts'))
            ->header('Content-Type', 'application/xml; charset=utf-8');
    }

    public function videos()
    {
        $domain = rtrim(Setting::get('seo_canonical_domain', config('app.url')), '/');

        $animes = Anime::where('is_published', true)
            ->with(['episodes' => fn($q) => $q->with('season:id,season_number')->orderBy('episode_number')->limit(1)])
            ->select('id', 'slug', 'title', 'description', 'cover_image', 'updated_at', 'rating')
            ->orderByDesc('rating')
            ->limit(200)
            ->get()
            ->each(function ($anime) use ($domain) {
                $anime->cover_image_url = $anime->cover_image
                    ? (str_starts_with($anime->cover_image, 'http') ? $anime->cover_image : $domain . '/storage/' . $anime->cover_image)
                    : null;
                // İlk bölümün izleme URL'si — player_loc için (loc'tan farklı, gerçek player sayfası)
                $firstEp = $anime->episodes->first();
                $sNum = $firstEp?->season?->season_number ?? 1;
                $eNum = $firstEp?->episode_number ?? 1;
                $anime->first_ep_watch_url = $domain . '/watch/' . $anime->slug . '/' . $sNum . '/' . $eNum;
            });

        return response()
            ->view('sitemaps.videos', compact('domain', 'animes'))
            ->header('Content-Type', 'application/xml; charset=utf-8');
    }
}
