import api from './api';

export type Category = {
  name: string;
  slug?: string;
  url?: string; // optional explicit URL override
  children?: { name: string; slug?: string; url?: string }[];
};

export type CustomNavLink = {
  label: string;
  url: string;
  external?: boolean;
};

export type PublicSettings = {
  frontpage_layout?: string;
  frontpage_style?: string;
  hero_style?: 'grid' | 'scroller' | 'swiper' | 'swiper_full';
  club_id?: string;
  club_type?: 'football' | 'futsal';
  club_name?: string; // preferred display name
  club_logo_url?: string; // high-quality PNG/SVG override
  primary_color?: string;
  secondary_color?: string;
  accent_color?: string;
  background_color?: string;
  text_color?: string;
  shop_url?: string; // external e-shop URL
  sponsors_layout?: 'grid' | 'slider' | 'scroller' | 'pyramid';
  sponsors_theme?: 'dark' | 'light';
  // Simple array of video URLs (legacy)
  videos?: string[];
  // Rich videos (preferred)
  videos_items?: Array<{
    url: string;
    title?: string;
    length?: string; // e.g. 3:45
    uploaded_at?: string; // ISO date
    thumbnail_url?: string;
  }>;
  facebook_url?: string;
  instagram_url?: string;
  youtube_url?: string;
  zonerama_url?: string;
  // Videos module
  videos_module_enabled?: boolean;
  videos_style?: 'slider' | 'grid3' | 'grid';
  videos_source?: 'auto' | 'manual';
  videos_limit?: number;
  // Merch module
  merch_module_enabled?: boolean;
  merch_style?: 'grid' | 'slider';
  merch_source?: 'manual' | 'auto';
  merch_limit?: number;
  merch_items?: Array<{
    title?: string;
    image_url: string;
    url?: string;
  }>;
  // generic gallery support (preferred). If provided, Navbar uses this instead of zonerama_url
  gallery_url?: string;
  gallery_label?: string; // default: "Fotogalerie"
  font_heading?: string;
  font_body?: string;
  custom_css?: string;
  custom_js?: string;
  custom_html_home?: string;
  custom_html_blog_list?: string;
  custom_html_blog_post?: string;
  // About/Club page
  about_html?: string;
  show_about_in_nav?: boolean;
  // Custom navigation links managed via admin settings
  custom_nav?: CustomNavLink[];
  // categories for top navigation (optional)
  categories?: Category[];
  // Map settings
  map_style?: 'default' | 'positron' | 'positron-no-labels' | 'dark' | 'dark-no-labels' | 'toner' | 'toner-lite' | 'voyager' | 'terrain' | 'watercolor' | 'satellite';
};

export type AdminSettings = PublicSettings & {
  id?: number;
  created_at?: string;
  updated_at?: string;
  // SMTP configuration (admin-only)
  smtp_host?: string;
  smtp_port?: number;
  smtp_user?: string;
  smtp_password?: string;
  smtp_from?: string;
  smtp_from_name?: string;
  smtp_encryption?: 'tls' | 'ssl' | 'none';
  smtp_auth?: boolean;
  smtp_skip_verify?: boolean;
  // Newsletter defaults
  default_digest_type?: 'blogs' | 'events' | 'matches' | 'scores' | 'weekly';
  default_digest_competitions?: string;
  // Newsletter scheduling
  enable_weekly?: boolean;
  enable_match_reminders?: boolean;
  enable_results?: boolean;
  newsletter_weekly_day?: 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat';
  newsletter_weekly_hour?: number; // 0-23
  newsletter_reminder_lead_hours?: number; // default 48
  newsletter_quiet_start?: number; // 0-23
  newsletter_quiet_end?: number;   // 0-23
  // Contact & Map settings
  contact_address?: string;
  contact_city?: string;
  contact_zip?: string;
  contact_country?: string;
  contact_phone?: string;
  contact_email?: string;
  location_latitude?: number;
  location_longitude?: number;
  map_zoom_level?: number;
  show_map_on_homepage?: boolean;
  // Homepage matches display configuration
  finished_match_display_days?: number; // Number of days to show finished matches with scores on homepage
};

export const getPublicSettings = async (): Promise<PublicSettings> => {
  const res = await api.get('/settings');
  return res.data;
};

export const getAdminSettings = async (): Promise<AdminSettings> => {
  const res = await api.get('/admin/settings');
  return res.data;
};

export const updateAdminSettings = async (payload: Partial<AdminSettings>): Promise<AdminSettings> => {
  const res = await api.put('/admin/settings', payload);
  return res.data;
};
