import api, { ApiResponse } from './api';
import { ProfileField } from './profile-fields.service';

// Types
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;
  createdAt: string;
  updatedAt: string;
  fields?: ProfileField[];
  agentTypeSections?: AgentTypeSection[];
  _count?: {
    fields: number;
    agentTypeSections: number;
  };
}

export interface AgentTypeSection {
  id: string;
  agentTypeId: string;
  sectionId: string;
  sortOrder: number;
  isRequired: boolean;
  createdAt: string;
  updatedAt: string;
  agentType?: {
    id: string;
    name: string;
  };
  section?: ProfileSection;
}

export interface CreateSectionDto {
  name: string;
  description?: string;
  icon?: string;
  sortOrder?: number;
  isActive?: boolean;
  isGlobal?: boolean;
  isRepeatable?: boolean;
}

export interface UpdateSectionDto {
  name?: string;
  description?: string;
  icon?: string;
  sortOrder?: number;
  isActive?: boolean;
  isGlobal?: boolean;
  isRepeatable?: boolean;
}

export interface AssignSectionDto {
  agentTypeId: string;
  sortOrder?: number;
  isRequired?: boolean;
}

export interface UpdateAssignmentDto {
  sortOrder?: number;
  isRequired?: boolean;
}

export interface SectionOrderItem {
  sectionId: string;
  sortOrder: number;
  isRequired?: boolean;
}

export interface AgentTypeSectionsResponse {
  agentType: {
    id: string;
    name: string;
    description: string | null;
  };
  sections: (ProfileSection & {
    isRequired: boolean;
    isGlobal: boolean;
    effectiveSortOrder: number;
    hasCustomOrder: boolean;
  })[];
}

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

  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;
  }

  async getById(id: string): Promise<ProfileSection> {
    const response = await api.get<ApiResponse<ProfileSection>>(`${this.basePath}/${id}`);
    return response.data.data;
  }

  async create(data: CreateSectionDto): Promise<ProfileSection> {
    const response = await api.post<ApiResponse<ProfileSection>>(this.basePath, data);
    return response.data.data;
  }

  async update(id: string, data: UpdateSectionDto): Promise<ProfileSection> {
    const response = await api.patch<ApiResponse<ProfileSection>>(`${this.basePath}/${id}`, data);
    return response.data.data;
  }

  async delete(id: string): Promise<void> {
    await api.delete(`${this.basePath}/${id}`);
  }

  // Assignment methods
  async assignToAgentType(sectionId: string, data: AssignSectionDto): Promise<AgentTypeSection> {
    const response = await api.post<ApiResponse<AgentTypeSection>>(
      `${this.basePath}/${sectionId}/assign`,
      data
    );
    return response.data.data;
  }

  async updateAssignment(
    sectionId: string,
    agentTypeId: string,
    data: UpdateAssignmentDto
  ): Promise<AgentTypeSection> {
    const response = await api.patch<ApiResponse<AgentTypeSection>>(
      `${this.basePath}/${sectionId}/assignment/${agentTypeId}`,
      data
    );
    return response.data.data;
  }

  async removeFromAgentType(sectionId: string, agentTypeId: string): Promise<void> {
    await api.delete(`${this.basePath}/${sectionId}/assignment/${agentTypeId}`);
  }

  // Agent Type Sections methods
  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 updateSectionOrderForAgentType(
    agentTypeId: string,
    sectionOrders: SectionOrderItem[]
  ): Promise<{ success: boolean; updated: number }> {
    const response = await api.patch<ApiResponse<{ success: boolean; updated: number }>>(
      `${this.basePath}/agent-type/${agentTypeId}/order`,
      { sectionOrders }
    );
    return response.data.data;
  }
}

export const profileSectionsService = new ProfileSectionsService();
