import { esiFetch } from './fetch';
import { CharacterHelper, type Character } from '@/db';

export interface MailHeader {
  from: number; // From whom the mail was sent
  is_read: boolean; // is_read boolean
  labels: string[]; // maxItems: 25, minimum: 0, title: get_characters_character_id_mail_labels, uniqueItems: true, labels array
  mail_id: number; // mail_id integer
  recipients: {
    recipient_id: number; // recipient_id integer
    recipient_type: 'alliance' | 'character' | 'corporation' | 'mailing_list'; // recipient_type enum
  }[]; // maxItems: 52, minimum: 0, title: get_characters_character_id_mail_recipients, uniqueItems: true, recipients of the mail
  subject: string; // Mail subject
  timestamp: string; // When the mail was sent
}

// requires scope: esi-mail.read_mail.v1
export function getMailHeaders(character: Character) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.read_mail.v1')) return null;
  return esiFetch<MailHeader[]>(`/characters/${character.eveID}/mail/`, character);
}

export interface SendMail {
  approved_cost?: number; // approved_cost number
  body: string; // body string; max length 10000
  recipients: {
    recipient_id: number; // recipient_id integer
    recipient_type: 'alliance' | 'character' | 'corporation' | 'mailing_list'; // recipient_type enum
  }[]; // maxItems: 50, minimum: 1, title: post_characters_character_id_mail, recipients of the mail
  subject: string; // subject string; max length 1000
}

// requires scope: esi-mail.send_mail.v1
export function sendMail(character: Character, mail: SendMail) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.send_mail.v1')) return null;
  return esiFetch(`/characters/${character.eveID}/mail/`, character, {
    method: 'POST',
    body: JSON.stringify(mail),
  });
}

// requires scope: esi-mail.read_mail.v1
export function deleteMail(character: Character, mailID: number) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.organize_mail.v1')) return null;
  return esiFetch(`/characters/${character.eveID}/mail/${mailID}/`, character, {
    method: 'DELETE',
  });
}

export interface Mail {
  body: string; // body string
  from: number; // from integer
  labels: string[]; // labels array
  read: boolean; // read boolean
  subject: string; // subject string
  timestamp: string; // timestamp string
  recipients: {
    recipient_id: number; // recipient_id integer
    recipient_type: 'alliance' | 'character' | 'corporation' | 'mailing_list'; // recipient_type enum
  }[]; // recipients array
}

// requires scope: esi-mail.read_mail.v1
export function getMail(character: Character, mailID: number) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.read_mail.v1')) return null;
  return esiFetch<Mail>(`/characters/${character.eveID}/mail/${mailID}/`, character);
}

export interface MailMetadata {
  labels: string[]; // labels array
  read: boolean; // read boolean
}

// requires scope: esi-mail.organize_mail.v1
export function updateMailMetadata(character: Character, mailID: number, metadata: MailMetadata) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.organize_mail.v1')) return null;
  return esiFetch(`/characters/${character.eveID}/mail/${mailID}/`, character, {
    method: 'PUT',
    body: JSON.stringify(metadata),
  });
}

export interface MailLabels {
  labels: {
    color: number; // color integer
    label_id: number; // label_id integer
    name: string; // name string
    unread_count: number; // unread_count integer
  }[]; // labels array
  total_unread_count: number; // total_unread_count integer
}

// requires scope: esi-mail.read_mail.v1
export function getMailLabels(character: Character) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.read_mail.v1')) return null;
  return esiFetch<MailLabels>(`/characters/${character.eveID}/mail/labels/`, character);
}

export interface CreateMailLabel {
  color: number; // color integer
  name: string; // name string
}

// requires scope: esi-mail.organize_mail.v1
export function createMailLabel(character: Character, label: CreateMailLabel) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.organize_mail.v1')) return null;
  return esiFetch(`/characters/${character.eveID}/mail/labels/`, character, {
    method: 'POST',
    body: JSON.stringify(label),
  });
}

// requires scope: esi-mail.organize_mail.v1
export function deleteMailLabel(character: Character, labelID: number) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.organize_mail.v1')) return null;
  return esiFetch(`/characters/${character.eveID}/mail/labels/${labelID}/`, character, {
    method: 'DELETE',
  });
}

export interface MailingList {
  mailing_list_id: number; // mailing_list_id integer
  name: string; // name string
}

// requires scope: esi-mail.read_mail.v1
export function getMailingLists(character: Character) {
  if (!CharacterHelper.hasScope(character, 'esi-mail.read_mail.v1')) return null;
  return esiFetch<MailingList[]>(`/characters/${character.eveID}/mail/lists/`, character);
}
