using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using domain.Models.ResponseModels;
using Microsoft.AspNetCore.Mvc;
using presence.domain.Models;
using presence.domain.UseCase;

namespace presence_api.Controllers.PresenceController;

[ApiController]
[Route("api/[controller]")]
public class PresenceController: ControllerBase
{
    private readonly PresenceUseCase _presenceUseCase;

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

    [HttpGet]
    public ActionResult<IEnumerable<Presence>> GetPresence([FromQuery] int GroupId, 
            [FromQuery] string StartData, [FromQuery] string EndData, 
            [FromQuery] int UserId)
    {
        DateOnly startData;
        DateOnly endData;
        bool isParsed = DateOnly.TryParse(StartData, out startData);
        bool isParse = DateOnly.TryParse(EndData, out endData);
        if (!isParsed)
        {
            return BadRequest("Неверный формат даты начала.");
        }
        if (!isParse)
        {
            return BadRequest("Неверный формат даты конца.");
        }
        return Ok(_presenceUseCase.GetPresence(GroupId, startData, endData, UserId));
    }

    [HttpPost("~/AddPresence")]
    public ActionResult<IEnumerable<Presence>> PostPresence([FromQuery] int GroupId, 
    [FromQuery] string StartData, [FromQuery] string EndData, [FromQuery] int UserId)
    {
        return Ok(_presenceUseCase.AddPresenceByDate(StartData, EndData, GroupId));
    }

    [HttpDelete("~/DeleteByGroup")]
    public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByGroup([FromQuery] int GroupId)
    {
        return Ok(_presenceUseCase.DeletePresenceByGroup(GroupId));
    }

    [HttpDelete("~/DeleteByUser")]
    public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByUser([FromQuery] int UserId)
    {
        return Ok(_presenceUseCase.DeletePresenceByUser(UserId));
    }

    [HttpDelete("~/DeleteByDate")]
    public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByDate([FromQuery] string StartData, [FromQuery] string EndData)
    {
        DateOnly startData;
        DateOnly endData;
        bool isParsed = DateOnly.TryParse(StartData, out startData);
        bool isParse = DateOnly.TryParse(EndData, out endData);
        if (!isParsed)
        {
            return BadRequest("Неверный формат даты начала.");
        }
        if (!isParse)
        {
            return BadRequest("Неверный формат даты конца.");
        }
        return Ok(_presenceUseCase.DeletePresenceByDate(startData, endData));
    }

    [HttpPost("~/IsAttendence")]
    public ActionResult<PresenceResponse> UpdateAttendance([FromQuery] int FirstClass, 
            [FromQuery] int LastClass, [FromQuery] string Data, 
            [FromQuery] int UserId)
    {
        DateOnly data;
        bool isParsed = DateOnly.TryParse(Data, out data);
        if (!isParsed)
        {
            return BadRequest("Неверный формат даты.");
        }
        return Ok(_presenceUseCase.UncheckAttendence(FirstClass, LastClass, data, UserId));
    }
}
