using Microsoft.AspNetCore.Mvc;
using BoredCareers.Services.DatabaseService;
using BoredCareers.Entities;
using System.Web.Http;

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

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

        [HttpGet]
        public async Task<IActionResult> GetCompany(int CompanyID) {
            if (isLoggedIn()) {
                Company? company = await _databaseService.GetCompany(CompanyID);
                if (company != null) {
                    return Ok(company);
                }
            }
            return NotFound();
        }

        [HttpPost]
        public async Task<IActionResult> SetCompany([FromBody] Company company) {
            if (isLoggedIn()) {
                if (await isLoggedInUserEmployeeOf(company.ID)) {
                    await _databaseService.SetCompany(company);
                    return Ok();
                }
            }
            return NotFound();
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteCompany(int CompanyID) {
            if (isLoggedIn()) {
                if (await isLoggedInUserEmployeeOf(CompanyID)) {
                    await _databaseService.DeleteCompany(CompanyID);
                    return Ok();
                }
            }
            return NotFound();
        }

    }

}
