import { getDb } from "../db.ts";

export type PopupType = "game_release" | "promotion" | "announcement" | "limited_time";

export interface PopupNotification {
  id: number;
  title: string;
  description: string;
  type: PopupType;
  imageUrl?: string;
  ctaText: string;
  ctaLink: string;
  targetSegments: string[];
  startDate: Date;
  endDate: Date;
  priority: "low" | "medium" | "high";
  dismissible: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface PlayerPopupView {
  playerId: number;
  popupId: number;
  viewedAt: Date;
  dismissed: boolean;
  clicked: boolean;
}

const POPUP_TEMPLATES: Record<PopupType, { title: string; description: string }> = {
  game_release: {
    title: "🎮 New Game Released",
    description: "Check out our latest slot game with amazing features!",
  },
  promotion: {
    title: "🎉 Special Promotion",
    description: "Limited time offer - Don't miss out!",
  },
  announcement: {
    title: "📢 Announcement",
    description: "Important update about CoinKrazy",
  },
  limited_time: {
    title: "⏰ Limited Time Event",
    description: "This offer expires soon - Claim now!",
  },
};

export async function createPopup(
  popup: Omit<PopupNotification, "id" | "createdAt" | "updatedAt">
): Promise<{ success: boolean; popupId?: number; error?: string }> {
  try {
    // In production, this would insert into database
    console.log(`[POPUP] Created: ${popup.title}`);
    return {
      success: true,
      popupId: Math.floor(Math.random() * 10000),
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : "Unknown error",
    };
  }
}

export async function getActivePopups(
  playerId: number
): Promise<PopupNotification[]> {
  // Popups are managed by admin via the Admin Panel → Popup Management
  // Return empty array; admins can create real popups through the admin interface
  return [];
}

export async function recordPopupView(
  playerId: number,
  popupId: number,
  action: "viewed" | "dismissed" | "clicked"
): Promise<{ success: boolean }> {
  try {
    console.log(`[POPUP] Player ${playerId} - Popup ${popupId}: ${action}`);
    return { success: true };
  } catch (error) {
    return { success: false };
  }
}

export async function getPopupAnalytics(popupId: number): Promise<{
  views: number;
  dismissals: number;
  clicks: number;
  ctr: number;
}> {
  // In production, query from database
  const mockViews = 1000;
  const mockDismissals = 200;
  const mockClicks = 150;

  return {
    views: mockViews,
    dismissals: mockDismissals,
    clicks: mockClicks,
    ctr: (mockClicks / mockViews) * 100,
  };
}

export async function schedulePopup(
  popup: PopupNotification,
  scheduledTime: Date
): Promise<{ success: boolean; scheduledId?: string }> {
  try {
    console.log(
      `[POPUP] Scheduled: ${popup.title} at ${scheduledTime.toISOString()}`
    );
    return {
      success: true,
      scheduledId: `scheduled_${Date.now()}`,
    };
  } catch (error) {
    return { success: false };
  }
}

export async function deletePopup(popupId: number): Promise<{ success: boolean }> {
  try {
    console.log(`[POPUP] Deleted: ${popupId}`);
    return { success: true };
  } catch (error) {
    return { success: false };
  }
}

export async function updatePopup(
  popupId: number,
  updates: Partial<PopupNotification>
): Promise<{ success: boolean }> {
  try {
    console.log(`[POPUP] Updated: ${popupId}`);
    return { success: true };
  } catch (error) {
    return { success: false };
  }
}

export function getPopupTemplate(type: PopupType): { title: string; description: string } {
  return POPUP_TEMPLATES[type];
}
