import {
  DateTime,
  TranslatableString,
  User,
  UserGroupOrUser,
} from "ChillMainAssets/types";
import { Person } from "ChillPersonAssets/types";
import { Thirdparty } from "../../../../ChillThirdPartyBundle/Resources/public/types";
import { StoredObject } from "ChillDocStoreAssets/types";

interface MotiveBase {
  type: "ticket_motive";
  id: number;
  active: boolean;
  label: TranslatableString;
  makeTicketEmergency: TicketEmergencyState;
}

/**
 * Represent a motive with basic information and parent motive.
 *
 * Match the "read" and "read:children-to-parent" serializer groups.
 */
export interface MotiveWithParent extends MotiveBase {
  parent: MotiveWithParent | null;
}

/**
 * Represents a motive for a ticket, including details like emergency status, stored objects, and supplementary comments.
 *
 * Match the "read:extended" serializer group in MotiveNormalizer.
 */
export interface Motive extends MotiveBase {
  makeTicketEmergency: TicketEmergencyState;
  storedObjects: StoredObject[];
  supplementaryComments: { label: string }[];
  children: Motive[];
}

export type TicketState = "open" | "closed" | "close";

export type TicketEmergencyState = "yes" | "no";

interface TicketHistory<T extends string, D extends object> {
  event_type: T;
  at: DateTime;
  by: User;
  data: D;
}

export interface PersonHistory {
  type: "ticket_person_history";
  id: number;
  startDate: DateTime;
  endDate: null | DateTime;
  person: Person;
  removedBy: null;
  createdBy: User | null;
  createdAt: DateTime | null;
}

export interface MotiveHistory {
  type: "ticket_motive_history";
  id: number;
  startDate: null;
  endDate: null | DateTime;
  motive: MotiveWithParent;
  createdBy: User | null;
  createdAt: DateTime | null;
}

export interface Comment {
  type: "ticket_comment";
  id: number;
  content: string;
  createdBy: User | null;
  createdAt: DateTime | null;
  updatedBy: User | null;
  updatedAt: DateTime | null;
  deleted: boolean;
  supplementaryComments: { label: string }[];
}

export interface AddresseeHistory {
  type: "ticket_addressee_history";
  id: number;
  startDate: DateTime | null;
  addressee: UserGroupOrUser;
  endDate: DateTime | null;
  removedBy: User | null;
  createdBy: User | null;
  createdAt: DateTime | null;
  updatedBy: User | null;
  updatedAt: DateTime | null;
}

export interface AddresseeState {
  addressees: UserGroupOrUser[];
}

export interface PersonsState {
  persons: Person[];
}
export interface CallerState {
  new_caller: Person | Thirdparty | null;
}

export interface StateChange {
  new_state: TicketState;
}

export interface EmergencyChange {
  new_emergency: TicketEmergencyState;
}

export interface CreateTicketState {
  by: User;
}

export type AddCommentEvent = TicketHistory<"add_comment", Comment>;
export type SetMotiveEvent = TicketHistory<"set_motive", MotiveHistory>;
export type AddPersonEvent = TicketHistory<"add_person", PersonHistory>;
export type AddresseesStateEvent = TicketHistory<
  "addressees_state",
  AddresseeState
>;
export type CreateTicketEvent = TicketHistory<
  "create_ticket",
  CreateTicketState
>;
export type PersonStateEvent = TicketHistory<"persons_state", PersonsState>;
export type ChangeStateEvent = TicketHistory<"state_change", StateChange>;
export type EmergencyStateEvent = TicketHistory<
  "emergency_change",
  EmergencyChange
>;
export type CallerStateEvent = TicketHistory<"set_caller", CallerState>;

export type TicketHistoryLine =
  | AddPersonEvent
  | CreateTicketEvent
  | AddCommentEvent
  | SetMotiveEvent
  | AddresseesStateEvent
  | PersonStateEvent
  | ChangeStateEvent
  | EmergencyStateEvent
  | CallerStateEvent;

interface BaseTicket<
  T extends "ticket_ticket:simple" | "ticket_ticket:extended" =
    "ticket_ticket:simple",
> {
  type_extended: T;
  type: "ticket_ticket";
  id: number;
  externalRef: string;
  createdAt: DateTime | null;
  currentAddressees: UserGroupOrUser[];
  currentPersons: Person[];
  currentMotive: MotiveWithParent | null;
  currentState: TicketState | null;
  emergency: TicketEmergencyState | null;
  caller: Person | Thirdparty | null;
}

export interface TicketSimple extends BaseTicket<"ticket_ticket:simple"> {
  type_extended: "ticket_ticket:simple";
}

export interface Ticket extends BaseTicket<"ticket_ticket:extended"> {
  type_extended: "ticket_ticket:extended";
  createdBy: User | null;
  updatedAt: DateTime | null;
  updatedBy: User | null;
  history: TicketHistoryLine[];
}

export interface TicketFilters {
  byPerson: Person[];
  byCreator: User[];
  byAddressee: UserGroupOrUser[];
  byCurrentState: TicketState[];
  byCurrentStateEmergency: TicketEmergencyState[];
  byMotives: Motive[];
  byCreatedAfter: string;
  byCreatedBefore: string;
  byResponseTimeExceeded: boolean;
  byAddresseeToMe: boolean;
  byTicketId: number | null;
}

export interface TicketFilterParams {
  byPerson?: number[];
  byCreator?: number[];
  byAddressee?: number[];
  byAddresseeGroup?: number[];
  byCurrentState?: TicketState[];
  byCurrentStateEmergency?: TicketEmergencyState[];
  byMotives?: number[];
  byCreatedAfter?: string;
  byCreatedBefore?: string;
  byResponseTimeExceeded?: string;
  byAddresseeToMe?: boolean;
  byTicketId?: number | null;
}

export interface TicketInitForm {
  content: string;
  motive?: MotiveWithParent | null;
  addressees: UserGroupOrUser[];
  persons: Person[];
  caller: Person | null;
  emergency: TicketEmergencyState;
}
