import axiosClient from "./axiosClient";
import { Note } from "../types/note";

export const notesApi = {
  getAll: async (): Promise<Note[]> => {
    const { data } = await axiosClient.get<Note[]>("/notes");
    return data;
  },

  search: async (params: {
    q?: string;
    tag?: string;
    date?: string;
  }): Promise<Note[]> => {
    const { data } = await axiosClient.get<Note[]>("/notes/search", { params });
    return data;
  },

  create: async (note: { content: string; date: string; time: string; is_private?: 0 | 1 }) => {
    const { data } = await axiosClient.post("/notes", note);
    return data;
  },

  update: async (id: number, content: string, skipTimestamp?: boolean, is_private?: 0 | 1) => {
    const { data } = await axiosClient.put(`/notes/${id}`, {
      content,
      skipTimestamp,
      is_private,
    });
    return data;
  },

  delete: async (id: number) => {
    await axiosClient.delete(`/notes/${id}`);
  },

  pin: async (id: number) => {
    const { data } = await axiosClient.put(`/notes/${id}/pin`);
    return data;
  },

  archive: async (id: number) => {
    const { data } = await axiosClient.put(`/notes/${id}/archive`);
    return data;
  },

  unarchive: async (id: number) => {
    const { data } = await axiosClient.put(`/notes/${id}/unarchive`);
    return data;
  },

  uploadImages: async (noteId: number, files: File[]) => {
    const formData = new FormData();
    files.forEach((file) => formData.append("images", file));

    const { data } = await axiosClient.post(
      `/notes/${noteId}/images`,
      formData,
      {
        headers: { "Content-Type": "multipart/form-data" },
      }
    );
    return data;
  },

  uploadFiles: async (noteId: number, files: File[]) => {
    const formData = new FormData();
    files.forEach((file) => formData.append("files", file));

    const { data } = await axiosClient.post(
      `/notes/${noteId}/files`,
      formData,
      {
        headers: { "Content-Type": "multipart/form-data" },
      }
    );
    return data;
  },

  deleteImage: async (noteId: number, imageId: number) => {
    await axiosClient.delete(`/notes/${noteId}/images/${imageId}`);
  },

  deleteFile: async (noteId: number, fileId: number) => {
    await axiosClient.delete(`/notes/${noteId}/files/${fileId}`);
  },

  getArchived: async (): Promise<Note[]> => {
    const { data } = await axiosClient.get<Note[]>("/notes/archived");
    return data;
  },

  deleteArchived: async (id: number) => {
    await axiosClient.delete(`/notes/archived/${id}`);
  },

  deleteAllArchived: async (password: string) => {
    const { data } = await axiosClient.delete("/notes/archived/all", {
      data: { password },
    });
    return data;
  },

  getDataVersion: async (): Promise<{
    last_updated_at: string | null;
    last_created_at: string | null;
    total_notes: number;
    timestamp: number;
  }> => {
    const { data } = await axiosClient.get("/notes/version");
    return data;
  },

  // Публичный метод для получения заметок пользователя (не требует аутентификации)
  getPublicNotes: async (username: string): Promise<Note[]> => {
    const { data } = await axiosClient.get<Note[]>(`/public/user/${username}/notes`);
    return data;
  },
};

export interface Log {
  id: number;
  action_type: string;
  details: string | null;
  created_at: string;
}

export const logsApi = {
  getLogs: async (params: {
    action_type?: string;
    limit?: number;
    offset?: number;
  }): Promise<Log[]> => {
    const { data } = await axiosClient.get<Log[]>("/logs", { params });
    return data;
  },
};
