import routes from "@/routes";
import { AuthPayload } from "../types";
import { getAuthHeaders, getBaseHeaders } from "../utils";

export type GetPayload = AuthPayload;

export interface PatchPayload extends AuthPayload {
  user: Partial<UserPatch>;
}

export interface Response {
  user: User;
  meta?: {
    links: {
      self: string;
    };
  };
}

export interface UserPatch {
  locale: string;
  timezone: string;
  profile_attributes: Partial<
    Pick<
      UserProfile,
      "gender" | "full_name" | "relationship_status" | "birthday"
    > & {
      birthplace_id: null;
      birthplace_attributes: {
        address?: string;
        coords?: string;
      };
      remote_userpic_url: string;
    }
  >;
  daily_push_subs_attributes: [
    {
      time: string;
      daily_push_id: string;
    }
  ];
}

export interface User {
  id: string | null | undefined;
  username: string | null;
  email: string;
  locale: string;
  state: string;
  timezone: string;
  new_registration: boolean;
  stat: {
    last_online_at: string | null;
    prev_online_at: string | null;
  };
  profile: UserProfile;
  daily_push_subs: Subscription[];
}

export interface UserProfile {
  full_name: string | null;
  gender: string | null;
  birthday: string | null;
  birthplace: UserBirhplace | null;
  age: UserAge | null;
  sign: UserSign | null;
  userpic: UserPic | null;
  userpic_mime_type: string | undefined;
  relationship_status: string;
  human_relationship_status: string;
}

export interface UserAge {
  years: number;
  days: number;
}

export interface UserSign {
  house: number;
  ruler: string;
  dates: {
    start: {
      month: number;
      day: number;
    };
    end: {
      month: number;
      day: number;
    };
  };
  sign: string;
  char: string;
  polarity: string;
  modality: string;
  triplicity: string;
}

export interface UserPic {
  th: string;
  th2x: string;
  lg: string;
}

export interface UserBirhplace {
  id: string;
  address: string;
  coords: string;
}

export interface Subscription {
  id: string;
  daily_push_id: string;
  time: string;
  updated_at: string;
  created_at: string;
  last_sent_at: string | null;
}

export const createGetRequest = ({ token }: GetPayload): Request => {
  const url = new URL(routes.server.user());
  return new Request(url, { method: "GET", headers: getAuthHeaders(token) });
};

export const createPatchRequest = ({ token, user }: PatchPayload): Request => {
  const url = new URL(routes.server.user());
  const body = JSON.stringify({ user });
  return new Request(url, {
    method: "PATCH",
    headers: getAuthHeaders(token),
    body,
  });
};

export enum ESourceAuthorization {
  "aura.main" = "aura.main",
  "aura.palmistry" = "aura.palmistry",
  "aura.chat" = "aura.chat",
  "aura.moons" = "aura.moons",
  "aura.main.new" = "aura.main.new",
  "aura.palmistry.new" = "aura.palmistry.new",
  "aura.chats" = "aura.chats",
  "aura.compatibility.v2" = "aura.compatibility.v2",
  "aura.compatibility.v3" = "aura.compatibility.v3",
  "aura.compatibility.v4" = "aura.compatibility.v4",
  "aura.test.payment" = "aura.test.payment" // anonymous payment
}

export enum EGender {
  "male" = "male",
  "female" = "female",
  "other" = "other"
}

export enum ERelationshipStatus {
  "single" = "single",
  "relationship" = "relationship",
  "married" = "married",
  "complicated" = "complicated",
  "other" = "other"
}

export interface ICreateAuthorizeUser {
  name: string;
  birthdate: string | null;
  gender: EGender;
  birthplace: {
    address?: string;
    coords?: string;
  }
  relationship_status: ERelationshipStatus;
}

export interface ICreateAuthorizePayload {
  email: string;
  locale: string;
  timezone: string;
  source: ESourceAuthorization;
  profile?: Partial<ICreateAuthorizeUser>;
  partner?: Partial<Exclude<ICreateAuthorizeUser, "relationship_status">>;
  sign?: boolean;
  signDate?: string;
  feature?: string;
}

export interface ICreateAuthorizeResponse {
  token: string;
  userId?: string;
  generatingVideo?: boolean;
  videoId?: string;
  authCode?: string;
}

export const createAuthorizeRequest = (data: ICreateAuthorizePayload): Request => {
  const body = JSON.stringify(data);
  return new Request(routes.server.dApiAuth(), {
    method: "POST",
    headers: getBaseHeaders(),
    body,
  });
}

export const createAuthorizeAnonymousRequest = (data: Omit<ICreateAuthorizePayload, "email">): Request => {
  const body = JSON.stringify(data);
  return new Request(routes.server.dApiAnonymousAuth(), {
    method: "POST",
    headers: getBaseHeaders(),
    body,
  });
}

export interface IMeResponse {
  user: IUser;
}

export interface IUser {
  ipLookup?: {
    country: string;
    region: string;
    eu: string;
    timezone: string;
    city: string;
  },
  profile: {
    birthplace: {
      address: string;
    },
    name: string;
    birthdate: string;
    gender: string;
    age: number;
    sign: string;
  },
  partner?: {
    birthplace: {
      address: string;
    },
    birthdate: string;
    gender: string;
    age: number;
    sign: string;
  },
  _id: string;
  initialIp?: string;
  sessionId?: string;
  email: string;
  locale: string;
  timezone: string;
  source: string;
  sign: boolean;
  signDate: string;
  password: string;
  externalId: string;
  klaviyoId: string;
  // stripeId: string | null;
  assistants: string[];
  createdAt: string;
  updatedAt: string;
}

export const createMeRequest = ({ token }: GetPayload): Request => {
  const url = new URL(routes.server.me());
  return new Request(url, { method: "GET", headers: getAuthHeaders(token) });
}
