<?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'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/convert'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/compress'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/resize'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/rotate-reflect'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/watermark'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/preferences'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/effects'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/autoremove'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/password'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/tools/exif-cleaner'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/pricing'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/blog'),
            'lastmod' => $now,
            'changefreq' => 'daily',
            'priority' => '0.8'
        ];

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

        $urls[] = [
            'loc' => URL::to('/affiliate'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/contact'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/terms-of-use'),
            'lastmod' => $now,
            'changefreq' => 'monthly',
            'priority' => '0.8'
        ];

        $urls[] = [
            'loc' => URL::to('/privacy-policy'),
            '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();
    }

}
