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

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("Job listing not found");
        }

        [HttpGet("company")]
        public async Task<IActionResult> GetCompanysJobListings([FromQuery] int CompanyID) {
            if (isLoggedIn()) {
                if (await isLoggedInUserEmployeeOf(CompanyID)) {
                    JobListing[] jobListings = await _databaseService.GetJobListingFromCompany(CompanyID);
                    return Ok(jobListings);
                }
                return NotFound("You are not an employee of company");
            }
            return NotFound("Not logged in");
        }

        [HttpGet]
        public async Task<IActionResult> GetJobListings(string? CC, string? PC, float? D, string? JT, bool? R, int? SMI, int? SMA, int Page = 1, int PageQuantity = 25 ) {

            // Get all relevant postal codes
            List<string> PostalCodes = new List<string>();
            if (PC != null && CC != null && D != null) {
                Location[] nearby = await _databaseService.GetNearbyLocations(PC, CC, D.Value);
                foreach (Location cur in nearby) {
                    PostalCodes.Add(cur.PostalCode);
                }
            } else if (PC != null) {
                PostalCodes.Add(PC);
            }

            string[]? pc = null;
            if (PostalCodes.Count > 0) {
                pc = PostalCodes.ToArray();
            }

            JobListing[] jobListings = await _databaseService.GetJobListingPage(Page, PageQuantity, pc, CC, JT, R, SMI, SMA );
            return Ok(jobListings);
        }

        [HttpPost]
        public async Task<IActionResult> SetJobListing([FromBody] JobListing JobListing) {
            if (isLoggedIn()) {
                if (await isLoggedInUserEmployeeOf(JobListing.CompanyID)) {
                    await _databaseService.SetJobListing(JobListing);
                    return Ok();
                }
                return NotFound("You are not an employee of company");
            }
            return NotFound("Not logged in");
        }

        [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 Ok();
                    }
                    return NotFound("You are not an employee of company");
                }
                return NotFound("Job listing doesn't exist already");
            }
            return NotFound("Not logged in");
        }

    }

}
