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

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

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

        [HttpGet("{JobListingID}")]
        public async Task<IActionResult> GetJobListing([FromRoute] int JobListingID) {
            var jobListing = await _databaseService.GetJobListing(JobListingID);
            if (jobListing != null) {
                return Ok(jobListing);
            }
            return NotFound();
        }

        [HttpGet]
        public async Task<IActionResult> GetJobListings(int Page = 1, int PageQuantity = 25) {
            JobListing[] jobListings = await _databaseService.GetJobListingPage(Page, PageQuantity);
            return Ok(jobListings);
        }

        [HttpPost]
        public async Task<IActionResult> SetJobListing([FromBody] JobListing JobListing) {
            if (isLoggedIn()) {
                if (await isLoggedInUserEmployeeOf(JobListing.CompanyID)) {
                    await _databaseService.SetJobListing(JobListing);
                }
            }
            return NotFound();
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteJobListing(int JobListingID) {
            if (isLoggedIn()) {
                JobListing? jobListing = await _databaseService.GetJobListing(JobListingID);
                if (jobListing != null) {
                    if (await isLoggedInUserEmployeeOf(JobListingID)) {
                        await _databaseService.DeleteJobListing(JobListingID);
                    }
                }
            }
            return NotFound();
        }

    }

}
