// Base interfaces for reusable structures
interface NameLocalized {
  am: string;
  en: string;
}

interface Metadata {
  uploadedBy: NameLocalized;
  deliveredBy: NameLocalized;
  organizationName: NameLocalized;
}

interface Position {
  id: string;
  name: NameLocalized;
}

interface Employee {
  id: string;
  name: NameLocalized;
}

interface Assigner {
  id: string;
  employee: Employee;
  delegator: string | null;
}

interface AssigneePosition {
  id: string;
  name: NameLocalized;
}

interface Assignee {
  id: string;
  employee?: Employee | null;
}

interface IncomingInstance {
  createdAt: string;
  id: string;
  amharicCreatedAt: string;
  reassignAt: string | null;
  amharicReassignedAt: string | null;
  respondedAt: string | null;
  amharicRespondedAt: string | null;
  repliedAt: string | null;
  amharicRepliedAt: string | null;
  status:
    | "accepted"
    | "re_assigned"
    | "draft"
    | "pending"
    | "completed"
    | string;
  isRead: boolean;
  comment: string | null;
  latestRemarkAt?: string | null;
  assigneeId?: string;
  assignee?: Assignee | null;
  assigneePositionId?: string;
  assigneePosition: AssigneePosition;
  assigner: Assigner;
  childAssignments?: {
    latestRemarkAt?: string | null;
  }[];
}

interface Content {
  id: string;
  subject: string;
}

interface Organization {
  id: string;
  name: NameLocalized;
}

interface Unit {
  id: string;
  name: NameLocalized;
  organization: Organization;
}

interface InitiatorPosition {
  id: string;
  name: NameLocalized;
}

interface Initiator {
  id: string;
  position: InitiatorPosition;
  employee: Employee;
  unit: Unit;
}

// Main Letter/Item DTO
interface IncomingLetterItem {
  id: string;
  referenceNumber: string;
  letterNumber: string | null;
  metadata: Metadata | null;
  dispatchedDate: string;
  amharicDispatchedDate: string;
  isUrgent: boolean;
  isDelegation: boolean;
  isLocked: boolean;
  isSecured: boolean;
  preferredLanguage?: string;
  initiationType: "self" | "external";
  recordTypeKey:
    | "non_smart_user"
    | "internal_memo"
    | "internal"
    | "external"
    | string;
  letterName: string;
  incomingInstances: IncomingInstance[];
  content: Content[];
  initiator: Initiator;
}

// Main Response DTO
interface IncomingRecordsResponse {
  count: number;
  items: IncomingLetterItem[];
}

// For component state management
interface IncomingRecordsState {
  letters: IncomingLetterItem[];
  isLoading: boolean;
  error: string | null;
  totalCount: number;
}

// Filter parameters DTO
interface IncomingRecordsFilters {
  searchTerm?: string;
  status?: IncomingInstance["status"];
  isUrgent?: boolean;
  startDate?: string;
  endDate?: string;
  recordType?: IncomingLetterItem["recordTypeKey"];
  page?: number;
  limit?: number;
}

// Form DTO for creating/updating letters
interface CreateLetterDTO {
  referenceNumber: string;
  letterNumber?: string | null;
  metadata?: Partial<Metadata>;
  dispatchedDate: string;
  isUrgent: boolean;
  isDelegation: boolean;
  initiationType: "self" | "external";
  recordTypeKey: IncomingLetterItem["recordTypeKey"];
  letterName: string;
  content: Omit<Content, "id">[];
  assigneePositionId: string;
  comment?: string;
}

// Update Letter DTO
interface UpdateLetterDTO extends Partial<CreateLetterDTO> {
  id: string;
}

// Instance action DTOs
interface AssignInstanceDTO {
  letterId: string;
  assigneePositionId: string;
  comment?: string;
}

interface ReassignInstanceDTO extends AssignInstanceDTO {
  instanceId: string;
}

interface RespondInstanceDTO {
  instanceId: string;
  response: string;
  comment?: string;
}

// Export all DTOs
export type {
  NameLocalized,
  Metadata,
  Position,
  Employee,
  Assigner,
  Assignee,
  AssigneePosition,
  IncomingInstance,
  Content,
  Organization,
  Unit,
  InitiatorPosition,
  Initiator,
  IncomingLetterItem,
  IncomingRecordsResponse,
  IncomingRecordsState,
  IncomingRecordsFilters,
  CreateLetterDTO,
  UpdateLetterDTO,
  AssignInstanceDTO,
  ReassignInstanceDTO,
  RespondInstanceDTO,
};

// Default export for convenience
export default IncomingRecordsResponse;
