import { siteconfig } from 'src/interfaces/site-config.interface';
import { dm, guglu, is_dm, is_guglu } from '../../constants/email';

/**
 * Database schema name for the application.
 * Falls back to 'westegmi_wosa_dbs_erp' if DB_SCHEMA environment variable is not set.
 */
export const DB_SCHEMA = process.env.DB_SCHEMA || 'device_management';

/**
 * Retrieves site-specific configuration based on the provided site identifier.
 * This function centralizes configuration management for multi-site/multi-tenant applications.
 *
 * @param site - The site identifier (e.g., 'guglu', 'dm')
 * @returns {siteconfig} Complete configuration object for the specified site
 *
 * @example
 * // Returns Guglu site configuration
 * const gugluConfig = getConfig('guglu');
 *
 * @example
 * // Returns DM site configuration
 * const dmConfig = getConfig('dm');
 *
 * @example
 * // Returns default empty configuration for unknown sites
 * const unknownConfig = getConfig('unknown');
 */
export const getConfig = (site: string): siteconfig => {
  switch (site) {
    case is_guglu: // Assuming is_guglu = 'guglu'
      return {
        // SMTP email configuration for Guglu
        config: guglu.mailConf,
        // Reply-to email address with name formatting
        replyTo: `${guglu.adminName}<${guglu.noReply}>`,
        // Site display name
        siteName: guglu.sitename,
        // Path to email templates directory
        template_path: guglu.template_path,
        // Base URL for the site
        url: guglu.url,
        // Primary admin email address
        adminEmail: guglu.adminEmail,
        // Password reset link format/template
        resetlink: guglu.resetlink,
      };
    // break statement is unreachable after return, could be removed

    case is_dm: // Assuming is_dm = 'dm'
      return {
        // SMTP email configuration for DM
        config: dm.mailConf,
        // Reply-to email address with name formatting
        replyTo: `${dm.adminName}<${dm.noReply}>`,
        // Site display name
        siteName: dm.sitename,
        // Path to email templates directory
        template_path: dm.template_path,
        // Base URL for the site
        url: dm.url,
        // Primary admin email address
        adminEmail: dm.adminEmail,
        // Note: Using Guglu's resetlink - verify if this is intentional
        resetlink: guglu.resetlink,
      };
    // break statement is unreachable after return, could be removed

    default:
      // Return safe default configuration for unknown/unsupported sites
      return {
        config: {
          auth: {
            user: '', // Empty SMTP username
            pass: '', // Empty SMTP password
          },
          host: '', // Empty SMTP host
          secure: false, // Default to non-secure connection
        },
        replyTo: '', // Empty reply-to address
        siteName: '', // Empty site name
        template_path: '', // Empty template path
        url: '', // Empty site URL
        adminEmail: '', // Empty admin email
        resetlink: '', // Empty reset link
      };
    // break statement is unreachable after return, could be removed
  }
};

/**
 * Generates a complete image URL for property listings.
 * Handles missing images and URL formatting.
 *
 * @param feature_image_url - Optional relative or absolute image URL
 * @returns {string} Complete image URL
 *
 * @example
 * // Returns default no-image URL
 * getPropertyImage(); // 'https://guglu.ca/static/images/noPic.jpg'
 *
 * @example
 * // Returns HTTPS URL unchanged
 * getPropertyImage('https://example.com/image.jpg'); // 'https://example.com/image.jpg'
 *
 * @example
 * // Converts relative URL to media server URL with resizing
 * getPropertyImage('uploads/image.jpg'); // 'https://media.guglu.ca/400xuploads/image.jpg'
 */
export const getPropertyImage = (feature_image_url?: string): string => {
  // Return default "no image" placeholder if no URL provided
  if (!feature_image_url) {
    return 'https://guglu.ca/static/images/noPic.jpg';
  }

  // Check if URL already uses HTTPS protocol
  const imageUrl = feature_image_url.startsWith('https:/')
    ? feature_image_url // Use HTTPS URL as-is
    : `https://media.guglu.ca/400x${feature_image_url}`; // Resize and serve from media server

  return imageUrl;
};

/**
 * Generates email logo URL based on provided email URL parameter.
 * Note: Function has unreachable code - the final return statement is never executed.
 *
 * @param email_url - Optional email-specific URL parameter (unused in current logic)
 * @returns {string} Complete logo URL
 *
 * @example
 * // Returns default PNG logo
 * getEmailLogo(); // 'http://localhost/aca-erp/.png'
 *
 * @example
 * // Returns mail logo regardless of email_url value
 * getEmailLogo('any-value'); // 'http://localhost/aca-erp/resources-f/images/mail_logo_v2.jpg'
 */
export const getEmailLogo = (email_url?: string): string => {
  const BASEURL = 'http://localhost/aca-erp/';

  if (!email_url) {
    // Return default logo path when no email_url provided
    // Note: '.png' appears to be a typo - likely should be a specific filename
    return `${BASEURL}.png`;
  } else {
    // Return specific mail logo regardless of email_url value
    return `${BASEURL}resources-f/images/mail_logo_v2.jpg`;
  }

  // This return statement is unreachable due to if-else always returning
  // Consider removing or restructuring the function
  return email_url;
};

/**
 * Recommendations for improvement:
 * 1. Remove unreachable code in getEmailLogo()
 * 2. Consider using environment variables for BASEURL in getEmailLogo()
 * 3. Add validation for site parameter in getConfig()
 * 4. Consider using a configuration object/map instead of switch statement
 * 5. Add error logging for unknown sites in getConfig()
 */
