import { authInstance, languageStore } from "@shared";
import { makeAutoObservable, runInAction } from "mobx";

export type Carrier = {
  id: number;
  short_name: string;
  full_name: string;
  slogan: string;
  city: string;
  city_id: number;
  logo: string;
  main_color: string;
  left_color: string;
  right_color: string;
};

type Carriers = {
  data: Carrier[];
  loaded: boolean;
};

type CashedCarrier = Record<number, Carrier>;

class CarrierStore {
  carriers: Carriers = {
    data: [],
    loaded: false,
  };
  carrier: CashedCarrier = {};

  constructor() {
    makeAutoObservable(this);
  }

  getCarriers = async () => {
    if (this.carriers.loaded) return;

    const response = await authInstance.get("/carrier");

    runInAction(() => {
      this.carriers.data = response.data;
      this.carriers.loaded = true;
    });
  };

  deleteCarrier = async (id: number) => {
    await authInstance.delete(`/carrier/${id}`);

    runInAction(() => {
      this.carriers.data = this.carriers.data.filter(
        (carrier) => carrier.id !== id
      );
      delete this.carrier[id];
    });
  };

  getCarrier = async (id: number) => {
    if (this.carrier[id]) return;
    const response = await authInstance.get(`/carrier/${id}`);

    runInAction(() => {
      if (!this.carrier[id]) {
        this.carrier[id] = {
          id: 0,
          short_name: "",
          full_name: "",
          slogan: "",
          city: "",
          city_id: 0,
          logo: "",
          main_color: "",
          left_color: "",
          right_color: "",
        };
      }
      this.carrier[id] = response.data;
    });
    return response.data;
  };

  createCarrier = async (
    fullName: string,
    shortName: string,
    city: string,
    cityId: number,
    main_color: string,
    left_color: string,
    right_color: string,
    slogan: string,
    logoId: string
  ) => {
    const response = await authInstance.post("/carrier", {
      full_name: fullName,
      short_name: shortName,
      city,
      city_id: cityId,
      main_color,
      left_color,
      right_color,
      slogan,
      logo: logoId,
    });
    runInAction(() => {
      this.carriers.data.push(response.data);
    });
  };

  editCarrierData = {
    full_name: "",
    short_name: "",
    city: "",
    city_id: 0,
    main_color: "",
    left_color: "",
    right_color: "",
    slogan: "",
    logo: "",
  };

  setEditCarrierData = (
    fullName: string,
    shortName: string,
    city: string,
    cityId: number,
    main_color: string,
    left_color: string,
    right_color: string,
    slogan: string,
    logoId: string
  ) => {
    this.editCarrierData = {
      full_name: fullName,
      short_name: shortName,
      city,
      city_id: cityId,
      main_color: main_color,
      left_color: left_color,
      right_color: right_color,
      slogan: slogan,
      logo: logoId,
    };
  };

  editCarrier = async (id: number) => {
    const response = await authInstance.patch(
      `/carrier/${id}`,
      this.editCarrierData
    );

    runInAction(() => {
      this.carriers.data = this.carriers.data.map((carrier) =>
        carrier.id === id ? { ...carrier, ...response.data } : carrier
      );

      this.carrier[id] = response.data;
    });
  };
}

export const carrierStore = new CarrierStore();
