using Demo.Domain.Models;
using Demo.Domain.UseCase;
using Microsoft.AspNetCore.Mvc;

namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]

public class PresenceController: ControllerBase{
    private readonly PresenceUseCase _presenceUseCase;

    public PresenceController(PresenceUseCase presenceUseCase)
    {
    _presenceUseCase = presenceUseCase;
    }

    //get

    [HttpGet]
    public ActionResult<PresenceGet> GetPresence(int groupID, string start = null, string end = null, Guid userGuid = default)
    {
        PresenceGet? presences = null;
        if (start != null && end != null)
        {
            DateOnly.TryParse(start, out DateOnly start1);
            DateOnly.TryParse(end, out DateOnly end1);
            presences = _presenceUseCase.GetPresencebyAll(groupID, start1, end1, userGuid);
        } else {
            presences = _presenceUseCase.GetPresencebyAll(groupID, null, null, userGuid);
        }
        return Ok(presences);
    }

    //post

    [HttpPost("generate")]
    public ActionResult<bool> GeneratePresence([FromBody] PresencePost request)
    {
        if (request == null){
            return BadRequest("buba");
        }

        DateOnly.TryParse(request.date, out DateOnly date);
        var isGenerated = _presenceUseCase.GeneratePresence(request.firstLesson, request.lastLesson, request.groupID, date);
        return Ok(isGenerated);
    }

    //delete

    [HttpDelete("gruop")]
    public ActionResult<bool> DeletePresenceByGroup(int groupID)
    {
        var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID);
        return isDeleted;
    }

    [HttpDelete("user")]
    public ActionResult<bool> DeletePresenceByUser(Guid userGuid)
    {
        var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid);
        return isDeleted;
    }

    [HttpDelete("range")]
    public ActionResult<bool> DeletePresenceByRange([FromBody] PresenceDeleteByRange request)
    {
        DateOnly.TryParse(request.start, out DateOnly start);
        DateOnly.TryParse(request.end, out DateOnly end);
        var isDeleted = _presenceUseCase.DeletePresenceByRange(start, end);
        return isDeleted;
    }

    //update

    [HttpPatch]
    public ActionResult<bool> UpdatePresence([FromBody] PresenceUpdate request)
    {
        DateOnly.TryParse(request.date, out DateOnly date);
        var isDeleted = _presenceUseCase.IsAttedance(request.firstLesson, request.lastLesson, date, request.UserGuid);
        return isDeleted;
    }
}