//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 connector

import (
	"encoding/json"
	"errors"
	"fmt"
	"htwkalender/ical/model"
	"log/slog"
)

func GetFeedByToken(host string, token string) (model.FeedRecord, error) {
	var feed model.FeedRecord

	// /api/collections/feeds/records/{id}

	response, err := RequestApi(host, "/api/collections/feeds/records/"+token)
	if err != nil || response.StatusCode() != 200 {
		slog.Error("Failed to get feed record", "error", err)
		return model.FeedRecord{}, fmt.Errorf("failed to get feed record")
	}

	// parse the response body json to FeedRecord struct
	feed, err = parseResponse(response.Body())
	if err != nil {
		slog.Error("Failed to read response body", "error", err)
		return model.FeedRecord{}, err
	}

	return feed, nil

}

func parseResponse(response []byte) (model.FeedRecord, error) {
	var feedRecord model.FeedRecord

	err := json.Unmarshal(response, &feedRecord)
	if err != nil {
		return model.FeedRecord{}, err
	}

	return feedRecord, nil
}

func DeleteFeedRecord(app model.AppType, token string) error {
	err := DeleteRequestApi(app.DataManagerURL, "/api/feed?token="+token)
	if err != nil {
		return err
	}

	return nil
}

func GetModuleWithEvents(app model.AppType, module model.FeedModule) (model.Module, error) {
	var modules model.Module

	response, err := RequestApi(app.DataManagerURL, "/api/module?uuid="+module.UUID)
	if err != nil {
		return model.Module{}, err
	}

	// parse the response body json to Events struct
	modules, err = parseModuleResponse(response.Body())
	if err != nil {
		slog.Error("Failed to read response body", "error", err)
		return model.Module{}, err
	}

	return modules, nil
}

func parseModuleResponse(body []byte) (model.Module, error) {
	var module model.Module

	err := json.Unmarshal(body, &module)
	if err != nil {
		return model.Module{}, err
	}

	return module, nil
}

func SaveFeedRecord(app model.AppType, modules []model.FeedCollection) (string, error) {
	var token string

	response, err := PostRequestApi(app.DataManagerURL, "/api/feed", modules)
	if err != nil {
		return "", err
	}

	if response.StatusCode() != 200 {
		return "", errors.New("failed to save feed")
	}

	// parse the response body json to string
	err = json.Unmarshal(response.Body(), &token)
	if err != nil {
		return "", err
	}

	return token, nil
}
