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

type City = {
  id: number;
  name: string;
  country_code: string;
  country: string;
  arms?: string;
};

class CityStore {
  cities: City[] = [];
  ruCities: City[] = [];
  city: City | null = null;

  constructor() {
    makeAutoObservable(this);
  }

  getCities = async () => {
    const response = await authInstance.get("/city");

    runInAction(() => {
      this.cities = response.data;
    });
  };

  getRuCities = async () => {
    const response = await languageInstance("ru").get("/city");

    runInAction(() => {
      this.ruCities = response.data;
    });
  };

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

    runInAction(() => {
      this.cities = this.cities.filter((city) => city.id !== id);
    });
  };

  getCity = async (id: string) => {
    const response = await authInstance.get(`/city/${id}`);

    runInAction(() => {
      this.city = response.data;
    });

    return response.data;
  };

  createCity = async (
    name: string,
    country: string,
    countryCode: string,
    mediaId: string
  ) => {
    const response = await authInstance.post("/city", {
      name: name,
      country: country,
      country_code: countryCode,
      arms: mediaId,
    });
    runInAction(() => {
      this.cities.push(response.data);
    });
  };
}

// export const cityStore = new CityStore();
