using System.Linq;
using c3stream.DataModels;
using c3stream.DataModels.Tables;
using LinqToDB;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace c3stream.Pages;

public class ConferenceModel : PageModel {
	private readonly ILogger<ConferenceModel> _logger;

	public ConferenceModel(ILogger<ConferenceModel> logger) => _logger = logger;

	public void OnGet() {
		var guid   = Request.Query["guid"].ToString();
		var state  = Request.Query["state"].ToString();
		var userid = Request.Cookies["bookmark"];
		if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark"))
			return;

		using var db       = new Database.DbConn();
		var       existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid);
		if (existing != null)
			if (state == "unwatched") {
				db.States.Delete(p => p == existing);
			}
			else {
				existing.State = state;
				db.Update(existing);
			}
		else
			db.Insert(new States { State = state, TalkId = guid, UserId = userid });

		Response.Redirect("/");
	}
}