package main

import (
	"path/filepath"

	"github.com/asdine/storm/v3"
)

// Location Routes

//SetupDatabase initializes the storm/bbolt database
func SetupDatabase(s *Server) (db *storm.DB) {
	db, err := storm.Open(filepath.Clean("./app/database/goInventorize.db"))
	if err != nil {
		s.Log.Fatal().Msgf("Unable to create/open database! %s", err)
	}
	return db
}

// AddLocation adds a location to the database
func (s *Server) AddLocation(loc Location) error {
	s.Log.Info().Msgf("Adding new location to database: ", loc)
	err := s.Database.Save(&loc)
	if err != nil {
		s.Log.Error().Msgf("Unable to add new location to database: %s error: %s ", loc, err)
		return err
	}
	return nil
}

// DeleteLocation deletes a location by ID
func (s *Server) DeleteLocation(loc Location) error {
	s.Log.Info().Msgf("Deleting location from database: ", loc)
	err := s.Database.DeleteStruct(&loc)
	if err != nil {
		s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", loc, err)
		return err
	}
	return nil
}

// GetLocations fetches all of the locations from the database
func (s *Server) GetLocations() (locations []Location, err error) {
	err = s.Database.All(&locations)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all locations: %s", err)
		return nil, err
	}
	return locations, nil
}

// GetLocation fetches a single location
func (s *Server) GetLocation(locID int) (location Location, err error) {
	err = s.Database.One("ID", locID, &location)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
		return location, err
	}
	return location, nil
}

// Room Routes

// AddRoom adds a room to a location
func (s *Server) AddRoom(room Room) error {
	s.Log.Info().Msgf("Adding new room to location: %s", room)
	err := s.Database.Save(&room)
	if err != nil {
		s.Log.Error().Msgf("Unable to add new room to database: %s error: %s", room, err)
		return err
	}
	return nil
}

// DeleteRoom deletes a room
func (s *Server) DeleteRoom(room Room) error {
	s.Log.Info().Msgf("Deleting room from database: %s", room)
	err := s.Database.DeleteStruct(&room)
	if err != nil {
		s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", room, err)
		return err
	}
	return nil
}

// GetAllRooms gets all of the rooms
func (s *Server) GetAllRooms() (rooms []Room, err error) {
	s.Log.Info().Msg("Getting all Rooms")
	err = s.Database.All(&rooms)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all room with error: %s", err)
		return rooms, err
	}
	return rooms, nil
}

// GetRoom gets a room based on id
func (s *Server) GetRoom(roomID int) (room Room, err error) {
	s.Log.Info().Msgf("Fetching room with id: %d", roomID)
	err = s.Database.One("ID", roomID, &room)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch room with id: %d with error: %s", roomID, err)
		return room, err
	}
	return room, nil
}

// GetRoomsAtLocation fetches all the rooms assigned to a location by location ID
func (s *Server) GetRoomsAtLocation(locID int) (rooms []Room, err error) {
	err = s.Database.Find("LocationID", locID, &rooms)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
		return nil, err
	}
	return rooms, nil
}

// Cabinet ROUTES

// AddCabinet adds a new cabinet
func (s *Server) AddCabinet(cabinet Cabinet) error {
	s.Log.Info().Msgf("Adding new cabinet to room: %s", cabinet)
	err := s.Database.Save(&cabinet)
	if err != nil {
		s.Log.Error().Msgf("Unable to add new cabinet to database: %s error: %s", cabinet, err)
		return err
	}
	return nil
}

// DeleteCabinet deletes a cabinet
func (s *Server) DeleteCabinet(cabinet Cabinet) error {
	s.Log.Info().Msgf("Deleting cabinet from database: %s", cabinet)
	err := s.Database.DeleteStruct(&cabinet)
	if err != nil {
		s.Log.Error().Msgf("Unable to delete room from database: %s error: %s ", cabinet, err)
		return err
	}
	return nil
}

// GetAllCabinets gets all of the cabinets
func (s *Server) GetAllCabinets() (cabinets []Cabinet, err error) {
	s.Log.Info().Msg("Getting all Cabinets")
	err = s.Database.All(&cabinets)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all cabinet with error: %s", err)
		return cabinets, err
	}
	return cabinets, nil
}

// GetCabinet gets a cabinet based on id
func (s *Server) GetCabinet(cabinetID int) (cabinet Cabinet, err error) {
	s.Log.Info().Msgf("Fetching cabinet with id: %d", cabinetID)
	err = s.Database.One("ID", cabinetID, &cabinet)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch cabinet with id: %d with error: %s", cabinetID, err)
		return cabinet, err
	}
	return cabinet, nil
}

// GetCabinetsInRoom fetches all of the cabinets assigned to a room
func (s *Server) GetCabinetsAtRoom(roomID int) (cabinets []Cabinet, err error) {
	err = s.Database.Find("Room", roomID, &cabinets)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all cabinets in room: %s", err)
		return nil, err
	}
	return cabinets, nil
}

// ITEM ROUTES

// GetItem gets an item by id
func (s *Server) GetItem(itemID int) (item Item, err error) {
	s.Log.Info().Msgf("Fetching item with id: %d", itemID)
	err = s.Database.One("ID", itemID, &item)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch item with id: %d with error: %s", itemID, err)
		return item, err
	}
	return item, nil
}

// GetAllItems gets all of the items
func (s *Server) GetAllItems() (items []Item, err error) {
	s.Log.Info().Msg("Getting all Items")
	err = s.Database.All(&items)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all items with error: %s", err)
		return items, err
	}
	return items, nil
}

// GetItemsInRoom fetches all of the items assigned to a room
func (s *Server) GetItemsInRoom(roomID int) (items []Item, err error) {
	err = s.Database.Find("Room", roomID, &items)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all items in room: %s", err)
		return nil, err
	}
	return items, nil
}

// GetItemsInRoom fetches all of the items assigned to a room
func (s *Server) GetItemsInCabinet(cabinetID int) (items []Item, err error) {
	err = s.Database.Find("Cabinet", cabinetID, &items)
	if err != nil {
		s.Log.Error().Msgf("Unable to fetch all items in cabinet: %s", err)
		return nil, err
	}
	return items, nil
}

// AddItem adds an item to a room
func (s *Server) AddItem(item Item) error {
	s.Log.Info().Msgf("Adding new item to room: ", item)
	err := s.Database.Save(&item)
	if err != nil {
		s.Log.Error().Msgf("Unable to add new item to room: %s error: %s", item, err)
		return err
	}
	return nil
}

// DeleteItem deletes an item
func (s *Server) DeleteItem(item Item) error {
	s.Log.Info().Msgf("Deleting Item from database: %s", item)
	err := s.Database.DeleteStruct(&item)
	if err != nil {
		s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", item, err)
		return err
	}
	return nil
}

// Misc calls

type SearchResults struct {
	Locations []Location `json:"locations"`
	Rooms     []Room     `json:"rooms"`
	Cabinets  []Cabinet  `json:"cabinets"`
	Items     []Item     `json:"items"`
}

// SearchAllByField searches the entire database (locs, rooms, cabinets, items) by the field and search term passed in
func (s *Server) SearchAllByField(fieldFilter string, searchTerm string) (results SearchResults, err error) {
	s.Log.Info().Msgf("Searching all items for term: %s", searchTerm)
	var locs []Location
	var rooms []Room
	var cabinets []Cabinet
	var items []Item
	results = SearchResults{}

	err = s.Database.Find(fieldFilter, searchTerm, &locs)
	if err != nil && err != storm.ErrNotFound {
		s.Log.Error().Msgf("Unable to search locations in database with term: %s :error: %s ", searchTerm, err)
		return results, err
	}
	err = s.Database.Find(fieldFilter, searchTerm, &rooms)
	if err != nil && err != storm.ErrNotFound {
		s.Log.Error().Msgf("Unable to search rooms in database with term: %s error: %s ", searchTerm, err)
		return results, err
	}
	err = s.Database.Find(fieldFilter, searchTerm, &cabinets)
	if err != nil && err != storm.ErrNotFound {
		s.Log.Error().Msgf("Unable to search cabinets in database with term: %s error: %s ", searchTerm, err)
		return results, err
	}
	err = s.Database.Find(fieldFilter, searchTerm, &items)
	if err != nil && err != storm.ErrNotFound {
		s.Log.Error().Msgf("Unable to search items in database with term: %s error: %s ", searchTerm, err)
		return results, err
	}

	results.Locations = locs
	results.Rooms = rooms
	results.Cabinets = cabinets
	results.Items = items

	return results, nil

}
