import api from './api';

// Field Types - matching backend enum
export type FieldType =
  | 'TEXT'
  | 'TEXTAREA'
  | 'CHECKBOX'
  | 'CHECKBOX_GROUP'
  | 'RADIO'
  | 'SELECT'
  | 'MULTI_SELECT'
  | 'RANGE'
  | 'NUMBER'
  | 'DATE'
  | 'TAG_INPUT'
  | 'FILE'
  | 'FILE_UPLOAD'
  | 'REPEATER';

// Types
export interface FieldOption {
  value: string;
  label: string;
}

export interface RangeConfig {
  min: number;
  max: number;
  step?: number;
  unit?: string;
}

export interface FieldValidation {
  min?: number;
  max?: number;
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  minDate?: string; // ISO date (YYYY-MM-DD) for DATE fields
  maxDate?: string; // ISO date (YYYY-MM-DD) for DATE fields
  allowedFormats?: string[];
  maxFileSize?: number;
}

export interface FieldUiConfig {
  columns?: number;
  showInPreview?: boolean;
  helpText?: string;
}

export interface ProfileField {
  id: string;
  sectionId: string;
  name: string;
  slug: string;
  fieldType: FieldType;
  description: string | null;
  placeholder: string | null;
  defaultValue: string | null;
  sortOrder: number;
  isActive: boolean;
  isRequired: boolean;
  isSearchableOnly: boolean;
  validation: FieldValidation | null;
  options: FieldOption[] | null;
  rangeConfig: RangeConfig | null;
  uiConfig: FieldUiConfig | null;
}

export interface ProfileSection {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  icon: string | null;
  sortOrder: number;
  isActive: boolean;
  isGlobal: boolean;
  isSystem: boolean;
  isRepeatable: boolean;
  fields?: ProfileField[];
  isRequired?: boolean;
  effectiveSortOrder?: number;
  hasCustomOrder?: boolean;
}

export interface AgentTypeSectionsResponse {
  agentType: {
    id: string;
    name: string;
    description: string | null;
  };
  sections: ProfileSection[];
}

interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

// Filterable field structure from backend
export interface FilterableField {
  slug: string;
  name: string;
  fieldType: string;
  options: FieldOption[];
}

// Profile Sections Service for Web
class ProfileSectionsService {
  private basePath = '/profile-sections';

  async getSectionsForAgentType(agentTypeId: string, includeFields = true): Promise<AgentTypeSectionsResponse> {
    const url = includeFields
      ? `${this.basePath}/agent-type/${agentTypeId}`
      : `${this.basePath}/agent-type/${agentTypeId}?includeFields=false`;
    const response = await api.get<ApiResponse<AgentTypeSectionsResponse>>(url);
    return response.data.data;
  }

  async getAll(includeInactive = false): Promise<ProfileSection[]> {
    const url = includeInactive
      ? `${this.basePath}?includeInactive=true`
      : this.basePath;
    const response = await api.get<ApiResponse<ProfileSection[]>>(url);
    return response.data.data;
  }

  /**
   * Get all filterable fields with options (for search filters).
   * When `agentTypeId` is provided, filters are scoped to that agent type's sections plus globals.
   */
  async getFilterableFields(agentTypeId?: string | null): Promise<FilterableField[]> {
    const response = await api.get<ApiResponse<FilterableField[]>>('/profile-fields/filterable', {
      params: agentTypeId ? { agentTypeId } : undefined,
    });
    return response.data.data;
  }
}

export const profileSectionsService = new ProfileSectionsService();
