import { create } from 'zustand';
import api from './api';

export interface Book {
  id: number;
  uid: string;
  isbn: string;
  title: string;
  author: string;
  category: string;
  publisher: string;
  year_published: number;
  total_copies: number;
  available_copies: number;
  shelf_location: string;
  status: string;
}

export interface Issue {
  id: number;
  uid: string;
  book_id: number;
  user_id: number;
  issue_date: string;
  due_date: string;
  return_date: string | null;
  status: 'issued' | 'returned';
  book_title: string;
  book_author: string;
  first_name: string;
  last_name: string;
  issuer_name: string;
}

export interface Reservation {
  id: number;
  uid: string;
  book_id: number;
  user_id: number;
  reservation_date: string;
  status: 'pending' | 'fulfilled' | 'cancelled';
  notes: string;
  book_title: string;
  book_author: string;
  first_name: string;
  last_name: string;
}

export interface Fine {
  id: number;
  uid: string;
  issue_id: number;
  user_id: number;
  amount: number;
  reason: string;
  status: 'unpaid' | 'paid' | 'waived';
  book_title: string;
  first_name: string;
  last_name: string;
}

export interface LibraryStore {
  books: Book[];
  issues: Issue[];
  reservations: Reservation[];
  fines: Fine[];
  loading: boolean;
  
  fetchBooks: () => Promise<void>;
  fetchIssues: () => Promise<void>;
  fetchReservations: () => Promise<void>;
  fetchFines: () => Promise<void>;
  
  addBook: (data: any) => Promise<void>;
  updateBook: (id: number, data: any) => Promise<void>;
  deleteBook: (id: number) => Promise<void>;
  
  issueBook: (data: any) => Promise<void>;
  returnBook: (id: number, data: any) => Promise<void>;
  
  reserveBook: (data: any) => Promise<void>;
  cancelReservation: (id: number) => Promise<void>;
  
  payFine: (id: number) => Promise<void>;
  waiveFine: (id: number) => Promise<void>;
}

export const useLibraryStore = create<LibraryStore>((set, get) => ({
  books: [],
  issues: [],
  reservations: [],
  fines: [],
  loading: false,

  fetchBooks: async () => {
    set({ loading: true });
    const res = await api.get('/library/books');
    set({ books: res.data, loading: false });
  },

  fetchIssues: async () => {
    const res = await api.get('/library/issues');
    set({ issues: res.data });
  },

  fetchReservations: async () => {
    const res = await api.get('/library/reservations');
    set({ reservations: res.data });
  },

  fetchFines: async () => {
    const res = await api.get('/library/fines');
    set({ fines: res.data });
  },

  addBook: async (data) => {
    await api.post('/library/books', data);
    get().fetchBooks();
  },

  updateBook: async (id, data) => {
    await api.put(`/library/books/${id}`, data);
    get().fetchBooks();
  },

  deleteBook: async (id) => {
    await api.delete(`/library/books/${id}`);
    get().fetchBooks();
  },

  issueBook: async (data) => {
    await api.post('/library/issues', data);
    get().fetchBooks();
    get().fetchIssues();
    get().fetchReservations(); // Auto-fulfill might happen
  },

  returnBook: async (id, data) => {
    await api.put(`/library/issues/${id}/return`, data);
    get().fetchBooks();
    get().fetchIssues();
    get().fetchFines();
  },

  reserveBook: async (data) => {
    await api.post('/library/reservations', data);
    get().fetchReservations();
  },

  cancelReservation: async (id) => {
    await api.put(`/library/reservations/${id}/cancel`);
    get().fetchReservations();
  },

  payFine: async (id) => {
    await api.post(`/library/fines/${id}/pay`);
    get().fetchFines();
  },

  waiveFine: async (id) => {
    await api.post(`/library/fines/${id}/waive`);
    get().fetchFines();
  }
}));