/**
 * Type definitions for careers module
 *
 * This file contains all TypeScript interfaces and types used in the careers system.
 * Centralizing types makes it easier to maintain consistency and catch errors early.
 */

/**
 * Supported languages for the platform
 */
export type Lang = "ar" | "en";

/**
 * All supported languages as a constant array
 */
export const SUPPORTED_LANGS: readonly Lang[] = ["en", "ar"] as const;

/**
 * Bilingual string translations
 * Every user-facing text field should use this format
 *
 * Example:
 * ```typescript
 * const title: StringTranslations = {
 *   en: "Senior Backend Engineer",
 *   ar: "مهندس باك-إند أول"
 * };
 * ```
 */
export type StringTranslations = Record<Lang, string>;

/**
 * Bilingual string array translations
 * Used for lists like requirements, skills, benefits
 *
 * Example:
 * ```typescript
 * const skills: StringArrayTranslations = {
 *   en: ["TypeScript", "Node.js"],
 *   ar: ["TypeScript", "Node.js"]
 * };
 * ```
 */
export type StringArrayTranslations = Record<Lang, string[]>;

/**
 * Job listing status
 * - draft: Not yet published
 * - open: Actively accepting applications
 * - closed: No longer accepting applications
 * - filled: Position has been filled
 */
export type JobStatus = "draft" | "open" | "closed" | "filled";

/**
 * Job contract type
 */
export type ContractType =
  | "full-time"
  | "part-time"
  | "contract"
  | "internship";

/**
 * Job experience level
 */
export type ExperienceLevel = "entry" | "mid" | "senior" | "lead" | "principal";

/**
 * Job department categories
 */
export type Department =
  | "engineering"
  | "product"
  | "design"
  | "sales"
  | "marketing"
  | "customer_success"
  | "operations"
  | "finance"
  | "hr";

/**
 * Complete job listing interface
 * This represents a job listing as it's used in the frontend
 *
 * All bilingual fields use StringTranslations or StringArrayTranslations
 * to ensure content is available in both Arabic and English
 */
export interface JobListing {
  // Identifiers
  id: string;
  slug: string; // URL-friendly identifier (e.g., "senior-backend-engineer")

  // Core info (bilingual)
  title: StringTranslations;
  departmentLabel: StringTranslations;
  location: StringTranslations;
  contractTypeLabel: StringTranslations;
  levelLabel: StringTranslations;
  salaryRange: StringTranslations;

  // Job description arrays (bilingual)
  benefits: StringArrayTranslations;
  skills: StringArrayTranslations;
  requirements: StringArrayTranslations;
  responsibilities: StringArrayTranslations;
  niceToHave: StringArrayTranslations;
  workdayExpectations: StringArrayTranslations;
  applicationInstructions: StringTranslations;

  // Metadata (not translated)
  department: string;
  contractType: string;
  level: string;
  status: string;

  // Dates
  openingAt: string; // ISO 8601 format
  closingAt: string | null;
  createdAt: string;
}

/**
 * Job record as stored in Supabase
 * This matches the database schema exactly
 *
 * Note: Uses snake_case (database convention) vs camelCase (TypeScript convention)
 * The transformation happens in the data layer
 */
export interface JobRecord {
  // Identifiers
  id: string;
  slug: string;

  // Bilingual fields (stored as JSONB in database)
  title_translations: Record<string, string>;
  department_label: Record<string, string>;
  location_translations: Record<string, string>;
  contract_type_label: Record<string, string>;
  level_label: Record<string, string>;
  compensation_translations: Record<string, string>;

  // Bilingual arrays (stored as JSONB in database)
  benefits: Record<string, string[]>;
  skills: Record<string, string[]>;
  requirements: Record<string, string[]>;
  responsibilities: Record<string, string[]>;
  nice_to_have: Record<string, string[]>;
  workday_expectations: Record<string, string[]>;
  application_instructions: Record<string, string>;

  // Metadata
  department: string;
  contract_type: string;
  level: string;
  status: string;

  // Timestamps
  opening_at: string;
  closing_at: string | null;
  created_at: string;
}

/**
 * Fallback values for string translations
 * Can be either a string (same for all langs) or partial translations
 */
export type StringFallback = string | Partial<Record<Lang, string>>;

/**
 * Database query error
 */
export interface DatabaseQueryError {
  code: string;
  message: string;
  details?: string | null;
  hint?: string | null;
}

/**
 * Result of fetching jobs
 * Includes both data and any error that occurred
 */
export interface JobFetchResult {
  jobs: JobListing[];
  error: DatabaseQueryError | null;
  count: number;
}
