using Appication;
using Domain;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Workhard.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ShipController : ControllerBase
    {
        protected readonly IShipService shipService;
        protected readonly SimpleContext context;
        


        public ShipController(IShipService x,
            SimpleContext context)
        {

            shipService = x;
            this.context = context;
            

        }

        

        [HttpGet]
        [Route("All")]

        public IActionResult Showallship()
        {
            return Ok(shipService.GetAllShip());
        }



        [HttpGet]
        [Route("Find")]

        public IActionResult Find(string ShipRegisterNo)
        {
            return Ok(shipService.GetShipByShipCode(ShipRegisterNo));
        }




        [HttpGet]
        [Route("Doc")]

        public IActionResult ShowRe()
        {
            return Ok(shipService.GetShowDoc());
        }


        [HttpPost]
        [Route("In")]

        public IActionResult InsertShip(CreateShip x)
        {
            shipService.InsertShip(x);
            return Ok();
        }

        [HttpPut]
        [Route("Up")]

        public IActionResult UpdateShip(UpdateShipModel x)
        {
            shipService.UpShip(x);
            return Ok();
        }


        [HttpDelete]
        [Route("{id}")]

        public IActionResult DeleteShip(int id)
        {
            shipService.DeleteShip(id);
            return Ok();
        }

       
       




        [HttpGet]
        [Route("Ben")]

        public List<MyShip> GetAllShip()
        {
            var emp = context.MyShips.Take(20).ToList();

            List<MyShip> results = new List<MyShip>();

            foreach (var e in emp)
            {
                results.Add(new MyShip() {

                    ShipRegisterNoKey = e.ShipRegisterNoKey,
                    Tha_Name = e.Tha_Name,
                    Eng_Name = e.Eng_Name,
                    TonGross = e.TonGross

                });
            }

            return results;
        }




        [HttpGet]
        [Route("Type")]

        public IActionResult Showdoc()
        {
            List<MyShip> emps = context.MyShips
                .Include(x => x.ShipCertificate)
                    .ThenInclude(x => x.Document)
                .Take(20)
                .ToList();
            List<ShipResponModel> result = new List<ShipResponModel>();

            foreach (var ship in emps)
            {
                var e = new ShipResponModel()
                {
                    
                    Tha_Name = ship.Tha_Name,
                    Document = ship.ShipCertificate.Select(x => new ShipDocument() { documentname=x.Document.DocumentName,documentnumber=x.certificateno}).ToList()
                    


                };

                result.Add(e);
            }

            return Ok(result);
        }


    }
}
