import { queryOne, run } from "@/lib/db"; export const dynamic = "force-dynamic"; // Disable caching // Helper function to ensure the hunt_status table exists async function ensureHuntStatusTableExists() { try { // Create hunt_status table if it doesn't exist await run(` CREATE TABLE IF NOT EXISTS hunt_status ( id INTEGER PRIMARY KEY CHECK (id = 1), is_active BOOLEAN DEFAULT 0, start_time TEXT, end_time TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) `); // Check if we need to insert the initial record const status = await queryOne("SELECT * FROM hunt_status LIMIT 1"); if (!status) { await run( "INSERT INTO hunt_status (id, is_active, start_time) VALUES (1, 0, NULL)" ); console.log("Initialized hunt_status table with default record"); } return true; } catch (error) { console.error("Error ensuring hunt_status table:", error); return false; } } // GET endpoint to check if the hunt is active - allow unauthenticated access for easier status checks export async function GET(request) { try { // First ensure the table exists await ensureHuntStatusTableExists(); // NO TOKEN CHECK - Allow anonymous status checks // This is critical for the waiting page to work reliably // Query all possible sources const huntStatus = await queryOne( "SELECT is_active, start_time FROM hunt_status LIMIT 1" ).catch(() => null); const timerStatus = await queryOne( "SELECT active, start_time, end_time FROM hunt_timer WHERE active = 1 ORDER BY id DESC LIMIT 1" ).catch(() => null); const huntActiveSetting = await queryOne( "SELECT value FROM settings WHERE key = 'hunt_active'" ).catch(() => null); // Also check the traditional hunts table const oldHuntRecord = await queryOne( "SELECT active, start_time FROM hunts ORDER BY id DESC LIMIT 1" ).catch(() => null); // Determine active status from all possible sources let isActive = false; let startTime = null; let endTime = null; // Check all possible active indicators in priority order if (timerStatus && Number(timerStatus.active) === 1) { console.log("Hunt active from timer table"); isActive = true; startTime = timerStatus.start_time; endTime = timerStatus.end_time; } else if (huntStatus && huntStatus.is_active) { console.log("Hunt active from hunt_status table"); isActive = true; startTime = huntStatus.start_time; } else if (huntActiveSetting && huntActiveSetting.value === "true") { console.log("Hunt active from settings table"); isActive = true; } else if (oldHuntRecord && oldHuntRecord.active) { console.log("Hunt active from hunts table"); isActive = true; startTime = oldHuntRecord.start_time; } console.log("Hunt status check:", { isActive, startTime, endTime }); // Set extremely aggressive cache control headers return new Response( JSON.stringify({ active: isActive, startTime: startTime || null, endTime: endTime || null, }), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0", Pragma: "no-cache", Expires: "0", }, } ); } catch (error) { console.error("Error checking hunt status:", error); return Response.json( { error: "Failed to check hunt status", active: false }, { status: 500 } ); } }