CREATE OR REPLACE VIEW optimized_sitemap AS
WITH
  sitemap_data AS (
    SELECT
      p.url::TEXT AS url
    FROM
      optimized_posts p
    UNION ALL
    SELECT
      b.url::TEXT AS url
    FROM
      optimized_books b
    UNION ALL
    SELECT
      m.url::TEXT AS url
    FROM
      optimized_movies m
    UNION ALL
    SELECT
      ar.url::TEXT AS url
    FROM
      optimized_artists ar
    UNION ALL
    SELECT
      g.url::TEXT AS url
    FROM
      optimized_genres g
    UNION ALL
    SELECT
      s.url::TEXT AS url
    FROM
      optimized_shows s
    UNION ALL
    SELECT
      pa.permalink::TEXT AS url
    FROM
      optimized_pages pa
    UNION ALL
    SELECT
      ss.slug AS url
    FROM
      static_slugs ss
    UNION ALL
    SELECT
      CONCAT('/tags/', LOWER(REPLACE(tag, ' ', '-'))) AS url
    FROM
      optimized_all_tags
    WHERE
      tag IS NOT NULL
      AND TRIM(tag) <> ''
  )
SELECT
  url
FROM
  sitemap_data;
