import axios from "axios";
import { IOption } from "../../../../common/domain/entities/option";
import { ProfileDataSource } from "../../../domain/datasources/profile_datasource";
import { API_URL } from "../../../../common/const/api";
import { CategoryModel } from "../../model/categorie_model";
import { categoryModelToOption } from "../../utils/category";

export class ProfileDataSourceProd implements ProfileDataSource {
  private readonly lang: string;
  constructor(lang: string) {
    this.lang = lang;
  }
  async getUserInterests(): Promise<IOption[]> {
    const {data, status} = await axios.get<CategoryModel[]>(`${API_URL}/user/prefered-categories?lang=${this.lang}`);
    if (status !== 200) {
      throw new Error("Error getting user interests");
    }
    const categories = data.map(categoryModelToOption);
    return categories.map((category) => ({
      ...category,
      isSelected: true,
    }));
  }
  async getInterests(): Promise<IOption[]> {
    //TODO: Implement this method
    const { status, data } = await axios.get<CategoryModel[]>(
      `${API_URL}/category?lang=${this.lang}`
    );
    if (status !== 200) {
      throw new Error("Error getting interests");
    }
    return data.map(categoryModelToOption);
  }

  async saveInterests(options: IOption[]): Promise<void> {
    const payload = {
      idCategories: options.map((option) => option.id),
    };
    const { status } = await axios.patch(
      `${API_URL}/user/update-prefered-categories`,
      payload
    );

    if (status !== 200) {
      throw new Error("Error updating interests");
    }
  }

  async setUpProfile(birthdate: string, interests: IOption[]): Promise<void> {
    throw new Error("Method not implemented.");
  }

  async changePassword(
    oldPassword: string,
    newPassword: string
  ): Promise<void> {
    const { status, data } = await axios.patch(
      `${API_URL}/user/change-password`,
      {
        prevPassword: oldPassword,
        newPassword: newPassword,
      }
    );
    console.log(status, data);
  }
}
