export type ExperienceSignalStatus = "green" | "yellow" | "red";
export type ExperienceTrendDirection = "up" | "down" | "flat";
export type ExperienceFindingPriority = "low" | "medium" | "high" | "critical";
export type ExperienceFindingStatus = "open" | "in_progress" | "resolved" | "closed";
export type ExperienceTemplateState = "draft" | "published" | "archived";
export type ExperienceScoreAggregationMode = "weighted_recent" | "moving_average" | "full_average";
export type ExperienceEvidenceKind = "photo" | "document" | "link";

export type ExperienceLocationSummary = {
  id: string;
  code: string;
  name: string;
  city: string | null;
  managerName: string | null;
  score: number;
  signal: ExperienceSignalStatus;
  trendDelta: number;
  trendDirection: ExperienceTrendDirection;
  lastEvaluationAt: string | null;
  totalEvaluations: number;
  openFindings: number;
};

export type ExperiencePolicyDTO = {
  id: string;
  name: string;
  isActive: boolean;
  aggregationMode: ExperienceScoreAggregationMode;
  recentWindow: number;
  recentWeights: number[];
  greenThreshold: number;
  yellowThreshold: number;
  updatedAt: string;
};

export type ExperienceOverviewResponse = {
  generatedAt: string;
  policy: ExperiencePolicyDTO;
  locations: ExperienceLocationSummary[];
};

export type ExperienceTemplateItemDTO = {
  id: string;
  key: string;
  label: string;
  weight: number;
  sortOrder: number;
  allowsComment: boolean;
  requiresObservation: boolean;
  allowsEvidence: boolean;
  isActive: boolean;
};

export type ExperienceTemplateCategoryDTO = {
  id: string;
  key: string;
  name: string;
  weight: number;
  sortOrder: number;
  items: ExperienceTemplateItemDTO[];
};

export type ExperienceTemplateDTO = {
  id: string;
  name: string;
  version: number;
  state: ExperienceTemplateState;
  isDefault: boolean;
  publishedAt: string | null;
  updatedAt: string;
  categories: ExperienceTemplateCategoryDTO[];
};

export type ExperienceEvaluationResponseDTO = {
  id: string;
  categoryId: string;
  itemId: string;
  score: number;
  scorePct: number;
  comment: string | null;
  observation: string | null;
  hasObservation: boolean;
  categoryLabelSnapshot: string;
  itemLabelSnapshot: string;
};

export type ExperienceEvaluationDTO = {
  id: string;
  locationId: string;
  templateId: string;
  createdByName: string | null;
  evaluatedAt: string;
  totalScore: number;
  signal: ExperienceSignalStatus;
  generalObservations: string;
  strengths: string;
  improvementAreas: string;
  responses: ExperienceEvaluationResponseDTO[];
};

export type ExperienceCategoryScoreRow = {
  categoryId: string;
  categoryKey: string;
  categoryName: string;
  avgScorePct: number;
  avgScoreRaw: number;
  responses: number;
};

export type ExperienceTrendPoint = {
  evaluationId: string;
  evaluatedAt: string;
  score: number;
};

export type ExperienceFindingDTO = {
  id: string;
  locationId: string;
  evaluationId: string | null;
  title: string;
  description: string;
  categoryKey: string | null;
  categoryLabel: string | null;
  priority: ExperienceFindingPriority;
  status: ExperienceFindingStatus;
  dueDate: string | null;
  resolvedAt: string | null;
  responsibleUserId: string | null;
  responsibleName: string | null;
  createdByName: string | null;
  createdAt: string;
  updatedAt: string;
};

export type ExperienceEvidenceDTO = {
  id: string;
  locationId: string;
  evaluationId: string | null;
  findingId: string | null;
  responseId: string | null;
  kind: ExperienceEvidenceKind;
  title: string;
  note: string | null;
  url: string | null;
  hasFile: boolean;
  mimeType: string | null;
  sizeBytes: number | null;
  createdAt: string;
  updatedAt: string;
  uploaderName: string | null;
};

export type ExperienceLocationDetailResponse = {
  location: ExperienceLocationSummary;
  template: ExperienceTemplateDTO | null;
  evaluations: ExperienceEvaluationDTO[];
  categoryBreakdown: ExperienceCategoryScoreRow[];
  trend: ExperienceTrendPoint[];
  findings: ExperienceFindingDTO[];
};

export type ExperienceEvaluationCreateResponseInput = {
  itemId: string;
  score: number;
  comment?: string;
  observation?: string;
  createFinding?: boolean;
  findingTitle?: string;
  findingDescription?: string;
  findingPriority?: ExperienceFindingPriority;
  responsibleUserId?: string;
};

export type ExperienceEvaluationCreatePayload = {
  locationId: string;
  templateId?: string;
  evaluatedAt?: string;
  generalObservations?: string;
  strengths?: string;
  improvementAreas?: string;
  responses: ExperienceEvaluationCreateResponseInput[];
};

export type ExperienceFindingCreatePayload = {
  locationId: string;
  evaluationId?: string;
  title: string;
  description?: string;
  categoryKey?: string;
  categoryLabel?: string;
  priority?: ExperienceFindingPriority;
  status?: ExperienceFindingStatus;
  dueDate?: string;
  responsibleUserId?: string;
};

export type ExperienceFindingPatchPayload = {
  title?: string;
  description?: string;
  priority?: ExperienceFindingPriority;
  status?: ExperienceFindingStatus;
  dueDate?: string | null;
  responsibleUserId?: string | null;
};

export type ExperienceScoringPolicyPatchPayload = {
  name?: string;
  aggregationMode?: ExperienceScoreAggregationMode;
  recentWindow?: number;
  recentWeights?: number[];
  greenThreshold?: number;
  yellowThreshold?: number;
  isActive?: boolean;
};

export type ExperienceTemplateSavePayload = {
  name?: string;
  state?: ExperienceTemplateState;
  isDefault?: boolean;
  categories?: Array<{
    id?: string;
    key: string;
    name: string;
    weight?: number;
    sortOrder?: number;
    items: Array<{
      id?: string;
      key: string;
      label: string;
      weight?: number;
      sortOrder?: number;
      allowsComment?: boolean;
      requiresObservation?: boolean;
      allowsEvidence?: boolean;
      isActive?: boolean;
    }>;
  }>;
  publish?: boolean;
};
