/**
 * Ad Ops System Configuration
 * Publications, placement zones, sender whitelist, rules
 */

export const ADOPS_MONITORED_EMAIL = 'adops@avbeat.com';
export const RYAN_EMAIL = 'ryan.salazar@relevantmediaproperties.com';
export const JEFF_EMAIL = 'jeff@avbeat.com';

export const SENDER_WHITELIST = [
  'jeff@avbeat.com',
  'sales@avbeat.com',
  'ryan.salazar@relevantmediaproperties.com',
];

export const APPROVAL_KEYWORDS = [
  'approved',
  'approve',
  'looks good',
  'go ahead',
  'go live',
  'lgtm',
];

export const BLACKMAGIC_KEYWORDS = ['blackmagic', 'blackmagic design', 'bmd'];

export type PublicationId = 'broadcast-beat' | 'av-beat';

export interface AdZone {
  id: string;
  label: string;
  width: number;
  height: number;
  altDimensions?: { width: number; height: number }[];
}

export interface Publication {
  id: PublicationId;
  name: string;
  shortName: string;
  domain: string;
  zones: AdZone[];
  enabled: boolean;
  isAdOpsOnly?: boolean;
}

export const PUBLICATIONS: Publication[] = [
  {
    id: 'broadcast-beat',
    name: 'AV Beat',
    shortName: 'BB',
    domain: 'avbeat.com',
    enabled: true,
    zones: [
      { id: 'bb_leaderboard_top', label: 'Leaderboard Top', width: 728, height: 90 },
      { id: 'bb_sidebar_top', label: 'Sidebar Top', width: 300, height: 250 },
      { id: 'bb_in_article_1', label: 'In-Article 1', width: 728, height: 90, altDimensions: [{ width: 300, height: 250 }] },
      { id: 'bb_in_article_2', label: 'In-Article 2', width: 300, height: 250 },
      { id: 'bb_footer', label: 'Footer', width: 728, height: 90 },
      { id: 'bb_mobile_top', label: 'Mobile Top', width: 320, height: 50 },
    ],
  },
  {
    id: 'av-beat',
    name: 'AV Beat',
    shortName: 'AV',
    domain: 'avbeat.com',
    enabled: true,
    zones: [
      { id: 'av_leaderboard_top', label: 'Leaderboard Top', width: 728, height: 90 },
      { id: 'av_sidebar_top', label: 'Sidebar Top', width: 300, height: 250 },
      { id: 'av_in_article_1', label: 'In-Article 1', width: 728, height: 90, altDimensions: [{ width: 300, height: 250 }] },
      { id: 'av_footer', label: 'Footer', width: 728, height: 90 },
      { id: 'av_mobile_top', label: 'Mobile Top', width: 320, height: 50 },
    ],
  },
];

export function getPublication(id: string): Publication | undefined {
  return PUBLICATIONS.find(p => p.id === id && p.enabled);
}

export function getZone(publicationId: string, zoneId: string): AdZone | undefined {
  const pub = getPublication(publicationId);
  return pub?.zones.find(z => z.id === zoneId);
}

export function validateBannerDimensions(
  publicationId: string,
  zoneId: string,
  width: number,
  height: number
): { valid: boolean; expected: string } {
  const zone = getZone(publicationId, zoneId);
  if (!zone) return { valid: false, expected: 'Unknown zone' };

  const primaryMatch = zone.width === width && zone.height === height;
  const altMatch = zone.altDimensions?.some(d => d.width === width && d.height === height);

  if (primaryMatch || altMatch) return { valid: true, expected: `${zone.width}x${zone.height}` };

  let expected = `${zone.width}x${zone.height}`;
  if (zone.altDimensions?.length) {
    expected += ` or ${zone.altDimensions.map(d => `${d.width}x${d.height}`).join(' or ')}`;
  }
  return { valid: false, expected };
}

export function isBlackmagicClient(clientName: string): boolean {
  const lower = clientName.toLowerCase();
  return BLACKMAGIC_KEYWORDS.some(kw => lower.includes(kw));
}

export function isApprovalReply(text: string): boolean {
  const lower = text.toLowerCase().trim();
  return APPROVAL_KEYWORDS.some(kw => lower.includes(kw));
}

export function isSenderWhitelisted(email: string): boolean {
  return SENDER_WHITELIST.includes(email.toLowerCase().trim());
}
