import { db } from '@/lib/db';
import { customPages } from '@/shared/schema';
import { eq, and } from 'drizzle-orm';

const SITE_ID = process.env.SITE_ID || 'ten-sura-game.com';

export const RESERVED_SLUGS = [
  '', 'about', 'about-us', 'privacy-policy', 'blogs', 'blog', 'api', 'category', 'author', 
  'page_editor_admin', '_next', 'robots.txt', 'sitemap.xml', 'icon.png', 'favicon.ico'
];

export async function getCustomPages() {
  try {
    const pages = await db
      .select()
      .from(customPages)
      .where(eq(customPages.siteId, SITE_ID))
      .orderBy(customPages.title);
    return pages;
  } catch (error) {
    console.error('Failed to fetch custom pages:', error);
    return [];
  }
}

export async function getCustomPageBySlug(slug: string) {
  try {
    const pages = await db
      .select()
      .from(customPages)
      .where(and(eq(customPages.siteId, SITE_ID), eq(customPages.slug, slug)))
      .limit(1);
    return pages[0] || null;
  } catch (error) {
    console.error('Failed to fetch custom page by slug:', error);
    return null;
  }
}

export async function getCustomPageById(id: number) {
  try {
    const pages = await db
      .select()
      .from(customPages)
      .where(and(eq(customPages.siteId, SITE_ID), eq(customPages.id, id)))
      .limit(1);
    return pages[0] || null;
  } catch (error) {
    console.error('Failed to fetch custom page by id:', error);
    return null;
  }
}
