//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
//Copyright (C) 2024 HTWKalender support@htwkalender.de

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Affero General Public License for more details.

//You should have received a copy of the GNU Affero General Public License
//along with this program.  If not, see <https://www.gnu.org/licenses/>.

package db

import (
	"htwkalender/data-manager/model"
	"htwkalender/data-manager/service/functions"
	"strings"
	"time"

	"github.com/pocketbase/dbx"
	"github.com/pocketbase/pocketbase"
)

func GetRooms(app *pocketbase.PocketBase) ([]string, error) {

	var events []struct {
		Rooms  string `db:"Rooms" json:"Rooms"`
		Course string `db:"course" json:"Course"`
	}

	// get all rooms from event records in the events collection
	err := app.Dao().DB().Select("Rooms", "course").From("events").Distinct(true).All(&events)
	if err != nil {
		return nil, err
	}

	roomArray, err := clearAndSeparateRooms([]struct {
		Rooms  string
		Course string
	}(events))

	if err != nil {
		return nil, err
	}

	return roomArray, nil
}

func clearAndSeparateRooms(events []struct {
	Rooms  string
	Course string
}) ([]string, error) {
	var roomArray []string
	for _, event := range events {

		var room []string

		// sport rooms don't have to be separated
		if event.Course != "Sport" {
			//split rooms by comma, tab, newline, carriage return, semicolon, space and non-breaking space
			room = functions.SeperateRoomString(event.Rooms)
		} else {
			room = append(room, event.Rooms)
		}

		//split functions room by space and add each room to array if it is not already in there
		for _, r := range room {
			var text = strings.TrimSpace(r)
			if !functions.Contains(roomArray, text) && len(text) >= 1 {
				roomArray = append(roomArray, text)
			}
		}
	}
	return roomArray, nil
}

func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.Event, error) {
	var events []model.Event

	// get all events from event records in the events collection
	err := app.Dao().DB().Select("*").From("events").
		Where(dbx.Like("Rooms", room).Escape("_", "_")).
		AndWhere(dbx.Like("Start", date)).
		GroupBy("Week", "Start", "End", "Rooms").
		All(&events)
	if err != nil {
		return nil, err
	}
	return events, nil
}

func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.Event, error) {
	var events []model.Event

	fromDate, err := time.Parse("2006-01-02", from)
	if err != nil {
		return nil, err
	}
	toDate, err := time.Parse("2006-01-02", to)
	if err != nil {
		return nil, err
	}

	// get all events from event records in the events collection
	err = app.Dao().DB().Select("*").From("events").
		Where(dbx.Like("Rooms", room).Escape("_", "_")).
		AndWhere(dbx.Between("Start", fromDate, toDate)).
		GroupBy("Week", "Start", "End", "Rooms").
		All(&events)

	if err != nil {
		return nil, err
	}
	return events, nil
}
