using Microsoft.AspNetCore.Mvc;
using BoredCareers.Services.DatabaseService;
using BoredCareers.Entities;

namespace BoredCareers.Controllers {
    [ApiController]
    [Route("api/resume")]
    public class ResumeController : MistoxControllerBase {

        public ResumeController(DatabaseService db) : base(db) {}

        [HttpGet]
        public async Task<IActionResult> GetResume(int? ResumeID) {
            if (ResumeID != null) {
                Resume? resume = await _databaseService.GetResume(ResumeID.Value);
                if (resume != null) {
                    return Ok(resume);
                }
                return NotFound("Unable to find resume");
            } else {
                if (isLoggedIn()) {
                    int accountID = getLoggedInUserID();
                    Resume[] resumes = await _databaseService.GetResumes(accountID);
                    return Ok(resumes);
                }
                return NotFound("Not logged in");
            }
        }

        [HttpPost]
        public async Task<IActionResult> SetResume([FromBody] Resume resume) {
            if (isLoggedIn()) {
                int AccountID = getLoggedInUserID();
                if (resume.AccountID == AccountID) {
                    await _databaseService.SetResume(resume);
                    return Ok();
                }
                return NotFound("Resume doesn't exist or you are not the owner");
            }
            return NotFound("Not logged in");
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteResume(int ResumeID) {
            if (isLoggedIn()) {
                int accountID = getLoggedInUserID();
                Resume? resume = await _databaseService.GetResume(ResumeID);
                if (resume != null && resume.AccountID == accountID) {
                    await _databaseService.DeleteResume(ResumeID);
                    return Ok();
                }
                return NotFound("Resume doesn't exist or you are not the owner");
            }
            return NotFound("Not logged in");
        }

    }
}