using Lab9.Models;
using Lab9.Repositories;
using Microsoft.AspNetCore.Mvc;

namespace Lab9.Controllers
{
    [Route("/")]
    public class MainController : Controller
    {
        private readonly IRepository _repository;

        public MainController(IRepository repository)
        {
            _repository = repository;
        }

        public IActionResult Index()
        {
            return View("Views/Index.cshtml");
        }

        [Route("login")]
        [HttpGet]
        public IActionResult Login()
        {
            return View("Views/Login.cshtml");
        }

        [Route("login")]
        [HttpPost]
        public IActionResult Login(string username, string password)
        {
            if (_repository.ValidateUser(username, password))
            {
                HttpContext.Session.SetString("username", username);
                return RedirectToAction("admin");
            }
            ViewBag.Error = "Invalid username or password";
            return View("Views/Login.cshtml");
        }

        [Route("logout")]
        [HttpGet]
        public IActionResult Logout()
        {
            HttpContext.Session.Remove("username");
            return Redirect("/");
        }

        [Route("view")]
        [HttpGet]
        public IActionResult News()
        {
            return View("Views/View.cshtml", _repository.GetAllNews());
        }

        [Route("admin")]
        [HttpGet]
        public IActionResult Admin()
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return Redirect("/login");
            }
            return View("Views/Admin.cshtml",_repository.GetAllNews());
        }

        [Route("add")]
        [HttpGet]
        public IActionResult Add()
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return Redirect("/login");
            }
            return View("Views/Add.cshtml");
        }

        [Route("edit/{id}")]
        [HttpGet]
        public IActionResult Edit(int id)
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return Redirect("/login");
            }
            return View("Views/Edit.cshtml", _repository.GetNewsById(id));
        }

        [Route("edit")]
        [HttpPost]
        public IActionResult Edit(News news)
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return Redirect("/login");
            }
            if (_repository.UpdateNews(news))
            {
                return RedirectToAction("Admin");
            }
            ViewBag.Error = "Failed to update news";
            return View("Views/Edit.cshtml", news);
        }

        [Route("add")]
        [HttpPost]
        public IActionResult Add(News news)
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return Redirect("/login");
            }
            if (_repository.InsertNews(news))
            {
                return RedirectToAction("Admin");
            }
            ViewBag.Error = "Failed to add news";
            return View("Views/Add.cshtml", news);
        }

        [Route("getnews")]
        [HttpPost]
        public IActionResult GetNews(string category, string date)
        {
            if (category != null && date != null)
            {
                return Ok(_repository.GetAllNewsByCategoryAndDate(category, DateTime.Parse(date)));
            }
            if (category != null)
            {
                return Ok(_repository.GetAllNewsByCategory(category));
            }
            if (date != null)
            {
                return Ok(_repository.GetAllNewsByDate(DateTime.Parse(date)));
            }
            return Ok(_repository.GetAllNews());
        }


    }
}
