<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\BlogPost;
use Illuminate\Support\Facades\URL;
use Carbon\Carbon;

class SitemapController extends Controller
{
    public function sitemap(): mixed
    {
        $sitemap = $this->urls();
        return response($sitemap, 200)
            ->header('Content-Type', 'application/xml');
    }

    private function urls(): mixed
    {
        // Array to store URLs
        $urls = [];
        $now = Carbon::now()->toAtomString();

        // Add static pages
        $urls[] = [
            'loc' => URL::to('/'),
            'lastmod' => $now,
            'changefreq' => 'daily',
            'priority' => '1.0'
        ];

        $toolsSlug = pageSlug('tools') ?: 'tools';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $convertSlug = pageSlug('tools_convert') ?: 'convert';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$convertSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $compressSlug = pageSlug('tools_compress') ?: 'compress';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$compressSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $resizeSlug = pageSlug('tools_resize') ?: 'resize';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$resizeSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $rotateSlug = pageSlug('tools_rotate_reflect') ?: 'rotate-reflect';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$rotateSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $watermarkSlug = pageSlug('tools_watermark') ?: 'watermark';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$watermarkSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $preferencesSlug = pageSlug('tools_preferences') ?: 'preferences';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$preferencesSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $effectsSlug = pageSlug('tools_effects') ?: 'effects';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$effectsSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $autoremoveSlug = pageSlug('tools_autoremove') ?: 'autoremove';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$autoremoveSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $passwordSlug = pageSlug('tools_password') ?: 'password';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$passwordSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $exifSlug = pageSlug('tools_exif_cleaner') ?: 'exif-cleaner';
        $urls[] = [
            'loc' => URL::to("/{$toolsSlug}/{$exifSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $pricingSlug = pageSlug('pricing') ?: 'pricing';
        $urls[] = [
            'loc' => URL::to("/{$pricingSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $blogSlug = pageSlug('blog') ?: 'blog';
        $urls[] = [
            'loc' => URL::to("/{$blogSlug}"),
            'lastmod' => $now,
            'changefreq' => 'daily',
            'priority' => '0.8'
        ];

        // Add dynamic pages
        $postSlug = pageSlug('blog_inner');
        foreach (BlogPost::all() as $post) {
            $urls[] = [
                'loc' => URL::to("/{$postSlug}/{$post->slug}"),
                'lastmod' => $post->updated_at->toAtomString(),
                'changefreq' => 'weekly',
                'priority' => '0.9'
            ];
        }

        if (affiliateSetting('status') == 1) {
            $affiliateSlug = pageSlug('affiliate') ?: 'affiliate';
            $urls[] = [
                'loc' => URL::to("/{$affiliateSlug}"),
                'lastmod' => $now,
                'changefreq' => 'monthly',
                'priority' => '0.8'
            ];
        }

        $contactSlug = pageSlug('contact') ?: 'contact';
        $urls[] = [
            'loc' => URL::to("/{$contactSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $termsSlug = pageSlug('terms_of_use') ?: 'terms-of-use';
        $urls[] = [
            'loc' => URL::to("/{$termsSlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $privacySlug = pageSlug('privacy_policy') ?: 'privacy-policy';
        $urls[] = [
            'loc' => URL::to("/{$privacySlug}"),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        // Generate XML
        return $this->generateSitemap($urls);
    }

    private function generateSitemap($urls): mixed
    {
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset/>');
        $xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

        foreach ($urls as $url) {
            $urlTag = $xml->addChild('url');
            $urlTag->addChild('loc', $url['loc']);
            $urlTag->addChild('lastmod', $url['lastmod']);
            $urlTag->addChild('changefreq', $url['changefreq']);
            $urlTag->addChild('priority', $url['priority']);
        }

        return $xml->asXML();
    }

}
