import apiClient from '../../../shared/services/apiClient';

export interface PlatformCurrencyResponse {
  status: string;
  data: {
    currency: string;
    updated_at: string | null;
    updated_by: string | null;
  };
}

export interface UpdateCurrencyRequest {
  currency: string;
}

export interface StripeSettingsResponse {
  status: string;
  data: {
    stripe_secret_key: string;
    stripe_publishable_key: string;
    stripe_webhook_secret: string;
    stripe_secret_key_masked: string;
    stripe_webhook_secret_masked: string;
    has_secret_key: boolean;
    has_publishable_key: boolean;
    has_webhook_secret: boolean;
    updated_at?: string | null;
    updated_by?: string | null;
  };
  message?: string;
}

export interface UpdateStripeSettingsRequest {
  stripe_secret_key?: string;
  stripe_publishable_key?: string;
  stripe_webhook_secret?: string;
}

export interface PayPalSettingsResponse {
  status: string;
  data: {
    paypal_client_id: string;
    paypal_client_secret: string;
    paypal_mode: string;
    paypal_client_secret_masked: string;
    has_client_id: boolean;
    has_client_secret: boolean;
  };
}

export interface UpdatePayPalSettingsRequest {
  paypal_client_id?: string;
  paypal_client_secret?: string;
  paypal_mode?: string;
}

export interface BoricaSettingsResponse {
  status: string;
  data: {
    borica_terminal_id: string;
    borica_merchant_id: string;
    borica_private_key_path: string;
    borica_certificate_path: string;
    borica_gateway_url: string;
    borica_mode: string;
    borica_terminal_id_masked: string;
    borica_merchant_id_masked: string;
    has_terminal_id: boolean;
    has_merchant_id: boolean;
    has_private_key_path: boolean;
    has_certificate_path: boolean;
    updated_at?: string | null;
    updated_by?: string | null;
  };
}

export interface UpdateBoricaSettingsRequest {
  borica_terminal_id?: string;
  borica_merchant_id?: string;
  borica_private_key_path?: string;
  borica_certificate_path?: string;
  borica_gateway_url?: string;
  borica_mode?: string;
}

export interface SmtpSettingsResponse {
  status: string;
  data: {
    smtp_host: string;
    smtp_port: string;
    smtp_user: string;
    smtp_password: string;
    smtp_password_masked: string;
    smtp_from_email: string;
    smtp_from_name: string;
    smtp_use_tls: boolean;
    has_host: boolean;
    has_user: boolean;
    has_password: boolean;
    updated_at?: string | null;
    updated_by?: string | null;
  };
  message?: string;
}

export interface UpdateSmtpSettingsRequest {
  smtp_host?: string;
  smtp_port?: string;
  smtp_user?: string;
  smtp_password?: string;
  smtp_from_email?: string;
  smtp_from_name?: string;
  smtp_use_tls?: boolean;
}

export interface TestSmtpEmailRequest {
  email: string;
}

export interface TestSmtpEmailResponse {
  status: string;
  message: string;
  data: {
    recipient: string;
    sent_at: string;
  };
}

export interface CompanySettingsResponse {
  status: string;
  data: {
    company_name: string;
    company_tagline: string;
    company_logo_url: string;
    company_favicon_url: string;
    company_phone: string;
    company_email: string;
    company_address: string;
    tax_rate: number;
    chat_working_hours_start: number;
    chat_working_hours_end: number;
    bank_name?: string;
    bank_account_number?: string;
    bank_account_holder?: string;
    bank_code?: string;
    updated_at?: string | null;
    updated_by?: string | null;
  };
  message?: string;
}

export interface UpdateCompanySettingsRequest {
  company_name?: string;
  company_tagline?: string;
  company_phone?: string;
  company_email?: string;
  company_address?: string;
  tax_rate?: number;
  chat_working_hours_start?: number;
  chat_working_hours_end?: number;
  bank_name?: string;
  bank_account_number?: string;
  bank_account_holder?: string;
  bank_code?: string;
}

export interface UploadLogoResponse {
  status: string;
  message: string;
  data: {
    logo_url: string;
    full_url: string;
  };
}

export interface UploadFaviconResponse {
  status: string;
  message: string;
  data: {
    favicon_url: string;
    full_url: string;
  };
}

export interface UploadBoricaCertificateResponse {
  status: string;
  message: string;
  data: {
    file_path: string;
    file_type: string;
    filename: string;
  };
}

export interface RecaptchaSettingsResponse {
  status: string;
  data: {
    recaptcha_site_key: string;
    recaptcha_enabled: boolean;
  };
}

export interface RecaptchaSettingsAdminResponse {
  status: string;
  data: {
    recaptcha_site_key: string;
    recaptcha_secret_key: string;
    recaptcha_secret_key_masked: string;
    recaptcha_enabled: boolean;
    has_site_key: boolean;
    has_secret_key: boolean;
    updated_at?: string | null;
    updated_by?: string | null;
  };
}

export interface UpdateRecaptchaSettingsRequest {
  recaptcha_site_key?: string;
  recaptcha_secret_key?: string;
  recaptcha_enabled?: boolean;
}

export interface ThemeSettingsResponse {
  status: string;
  data: {
    theme_primary_color: string;
    theme_primary_light: string;
    theme_primary_dark: string;
    theme_primary_accent: string;
    theme_layout_mode: string; // 'dark' or 'light'
    updated_at?: string | null;
    updated_by?: string | null;
  };
  message?: string;
}

export interface UpdateThemeSettingsRequest {
  theme_primary_color?: string;
  theme_primary_light?: string;
  theme_primary_dark?: string;
  theme_primary_accent?: string;
  theme_layout_mode?: string; // 'dark' or 'light'
}

export interface VerifyRecaptchaRequest {
  token: string;
}

export interface VerifyRecaptchaResponse {
  status: string;
  data: {
    verified: boolean;
    score?: number;
    action?: string;
    error_codes?: string[];
    message?: string;
  };
}

const systemSettingsService = {
  
  getPlatformCurrency: async (): Promise<PlatformCurrencyResponse> => {
    const response = await apiClient.get<PlatformCurrencyResponse>(
      '/api/admin/system-settings/currency'
    );
    return response.data;
  },

  
  updatePlatformCurrency: async (
    currency: string
  ): Promise<PlatformCurrencyResponse> => {
    const response = await apiClient.put<PlatformCurrencyResponse>(
      '/api/admin/system-settings/currency',
      { currency }
    );
    return response.data;
  },

  
  getStripeSettings: async (): Promise<StripeSettingsResponse> => {
    const response = await apiClient.get<StripeSettingsResponse>(
      '/api/admin/system-settings/stripe'
    );
    return response.data;
  },

  
  updateStripeSettings: async (
    settings: UpdateStripeSettingsRequest
  ): Promise<StripeSettingsResponse> => {
    const response = await apiClient.put<StripeSettingsResponse>(
      '/api/admin/system-settings/stripe',
      settings
    );
    return response.data;
  },

  
  getPayPalSettings: async (): Promise<PayPalSettingsResponse> => {
    const response = await apiClient.get<PayPalSettingsResponse>(
      '/api/admin/system-settings/paypal'
    );
    return response.data;
  },

  
  updatePayPalSettings: async (
    settings: UpdatePayPalSettingsRequest
  ): Promise<PayPalSettingsResponse> => {
    const response = await apiClient.put<PayPalSettingsResponse>(
      '/api/admin/system-settings/paypal',
      settings
    );
    return response.data;
  },

  getBoricaSettings: async (): Promise<BoricaSettingsResponse> => {
    const response = await apiClient.get<BoricaSettingsResponse>(
      '/api/admin/system-settings/borica'
    );
    return response.data;
  },

  updateBoricaSettings: async (
    settings: UpdateBoricaSettingsRequest
  ): Promise<BoricaSettingsResponse> => {
    const response = await apiClient.put<BoricaSettingsResponse>(
      '/api/admin/system-settings/borica',
      settings
    );
    return response.data;
  },

  
  uploadBoricaCertificate: async (
    file: File,
    fileType: 'private_key' | 'certificate' = 'private_key'
  ): Promise<UploadBoricaCertificateResponse> => {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('file_type', fileType);
    
    const response = await apiClient.post<UploadBoricaCertificateResponse>(
      '/api/admin/system-settings/borica/upload-certificate',
      formData
    );
    return response.data;
  },

  
  getSmtpSettings: async (): Promise<SmtpSettingsResponse> => {
    const response = await apiClient.get<SmtpSettingsResponse>(
      '/api/admin/system-settings/smtp'
    );
    return response.data;
  },

  
  updateSmtpSettings: async (
    settings: UpdateSmtpSettingsRequest
  ): Promise<SmtpSettingsResponse> => {
    const response = await apiClient.put<SmtpSettingsResponse>(
      '/api/admin/system-settings/smtp',
      settings
    );
    return response.data;
  },

  
  testSmtpEmail: async (
    email: string
  ): Promise<TestSmtpEmailResponse> => {
    const response = await apiClient.post<TestSmtpEmailResponse>(
      '/api/admin/system-settings/smtp/test',
      { email }
    );
    return response.data;
  },

  
  getCompanySettings: async (): Promise<CompanySettingsResponse> => {
    const response = await apiClient.get<CompanySettingsResponse>(
      '/api/admin/system-settings/company'
    );
    return response.data;
  },

  
  updateCompanySettings: async (
    settings: UpdateCompanySettingsRequest
  ): Promise<CompanySettingsResponse> => {
    const response = await apiClient.put<CompanySettingsResponse>(
      '/api/admin/system-settings/company',
      settings
    );
    return response.data;
  },

  
  uploadCompanyLogo: async (
    file: File
  ): Promise<UploadLogoResponse> => {
    const formData = new FormData();
    formData.append('image', file);
    
    
    const response = await apiClient.post<UploadLogoResponse>(
      '/api/admin/system-settings/company/logo',
      formData
    );
    return response.data;
  },

  
  uploadCompanyFavicon: async (
    file: File
  ): Promise<UploadFaviconResponse> => {
    const formData = new FormData();
    formData.append('image', file);
    
    
    const response = await apiClient.post<UploadFaviconResponse>(
      '/api/admin/system-settings/company/favicon',
      formData
    );
    return response.data;
  },
};

const recaptchaService = {
  
  getRecaptchaSettings: async (): Promise<RecaptchaSettingsResponse> => {
    const response = await apiClient.get<RecaptchaSettingsResponse>(
      '/api/admin/system-settings/recaptcha'
    );
    return response.data;
  },

  
  getRecaptchaSettingsAdmin: async (): Promise<RecaptchaSettingsAdminResponse> => {
    const response = await apiClient.get<RecaptchaSettingsAdminResponse>(
      '/api/admin/system-settings/recaptcha/admin'
    );
    return response.data;
  },

  
  updateRecaptchaSettings: async (
    settings: UpdateRecaptchaSettingsRequest
  ): Promise<RecaptchaSettingsAdminResponse> => {
    const response = await apiClient.put<RecaptchaSettingsAdminResponse>(
      '/api/admin/system-settings/recaptcha',
      settings
    );
    return response.data;
  },

  
  verifyRecaptcha: async (
    token: string
  ): Promise<VerifyRecaptchaResponse> => {
    const response = await apiClient.post<VerifyRecaptchaResponse>(
      '/api/admin/system-settings/recaptcha/verify',
      { token }
    );
    return response.data;
  },
};

const themeService = {
  getThemeSettings: async (): Promise<ThemeSettingsResponse> => {
    const response = await apiClient.get<ThemeSettingsResponse>(
      '/api/admin/system-settings/theme'
    );
    return response.data;
  },

  updateThemeSettings: async (
    settings: UpdateThemeSettingsRequest
  ): Promise<ThemeSettingsResponse> => {
    const response = await apiClient.put<ThemeSettingsResponse>(
      '/api/admin/system-settings/theme',
      settings
    );
    return response.data;
  },
};

export default systemSettingsService;
export { recaptchaService, themeService };

