using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.WebUtilities;
using bitforum.Models;
using bitforum.Models.Page.Faq;

namespace bitforum.Controllers.Page.Faq
{
    [Authorize]
    [Route("Page/Faq")]
    public class ItemController : Controller
    {
        private readonly ILogger<ItemController> _logger;
        private readonly DefaultDbContext _db;
        private readonly string _IndexViewPath = "~/Views/Page/Faq/Item/Index.cshtml";
        private readonly string _WriteViewPath = "~/Views/Page/Faq/Item/Write.cshtml";
        private readonly string _EditViewPath = "~/Views/Page/Faq/Item/Edit.cshtml";
        private Dictionary<string, string> _queryString;

        public ItemController(ILogger<ItemController> logger, DefaultDbContext db)
        {
            _logger = logger;
            _db = db;
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            ViewBag.QueryString = QueryHelpers.ParseQuery(HttpContext.Request.QueryString.Value).ToDictionary(k => k.Key, v => string.Join(",", v.Value));
            base.OnActionExecuting(context);
        }

        [HttpGet("Item/{categoryID?}")]
        public IActionResult Index(int? categoryID, [FromQuery] int page = 1)
        {
            ViewBag.CategoryID = categoryID;

            // 분류 목록
            ViewBag.FaqCategories = _db.FaqCategory.Include(c => c.FaqItem).Where(c => c.IsActive).OrderBy(c => c.Order).ToList();

            // FAQ 목록
            var faqItems = _db.FaqItem.Include(c => c.FaqCategory).OrderBy(c => c.Order).AsQueryable();
            if (categoryID.HasValue)
            {
                faqItems = faqItems.Where(c => c.CategoryID == categoryID);
            }

            ViewBag.FaqItems = faqItems.ToList();
            ViewBag.Total = faqItems.Count();
            ViewBag.Pagination = new Pagination(ViewBag.Total, page, 20, null);

            return View(_IndexViewPath);
        }

        [HttpGet("Item/Write")]
        public IActionResult Write()
        {
            ViewBag.FaqCategory = _db.FaqCategory.Where(c => c.IsActive).OrderBy(c => c.Order).ToList();

            return View(_WriteViewPath);
        }
        
        [HttpPost("Item/Create")]
        public async Task<IActionResult> Create(FaqItem request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("유효성 검사에 실패하였습니다.");
                }

                if (request.CategoryID <= 0 || !_db.FaqCategory.Any(c => c.ID == request.CategoryID))
                {
                    throw new Exception("유효한 분류를 선택하세요.");
                }

                request.UpdatedAt = null;
                request.CreatedAt = DateTime.Now;

                _db.FaqItem.Add(request);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("FAQ 등록 중 오류가 발생했습니다.");
                }

                string message = "FAQ가 정상적으로 등록되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return Redirect("/Page/Faq/Item");
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return Write();
            }
        }

        [HttpGet("Item/{id}/Edit")]
        public async Task<IActionResult> Edit(int id)
        {
            try
            {
                if (id <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                var faqItem = await _db.FaqItem.FirstAsync(c => c.ID == id);
                if (faqItem is null)
                {
                    throw new Exception("FAQ 정보를 찾을 수 없습니다.");
                }

                // 분류 목록
                ViewBag.FaqCategory = new SelectList(_db.FaqCategory.Where(c => c.IsActive).OrderBy(c => c.Order).ToList(), "ID", "Subject", faqItem.CategoryID);

                return View(_EditViewPath, faqItem);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return Redirect("/Page/Faq/Item");
            }
        }

        [HttpPost("Item/Update")]
        public async Task<IActionResult> Update(FaqItem request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("유효성 검사에 실패하였습니다.");
                }

                if (request.CategoryID <= 0 || !_db.FaqCategory.Any(c => c.ID == request.CategoryID))
                {
                    throw new Exception("유효한 분류를 선택하세요.");
                }

                var faqItem = await _db.FaqItem.FirstAsync(c => c.ID == request.ID);
                if (faqItem is null)
                {
                    throw new Exception("FAQ 정보를 찾을 수 없습니다.");
                }

                faqItem.CategoryID = request.CategoryID;
                faqItem.Question = request.Question;
                faqItem.Answer = request.Answer;
                faqItem.IsActive = request.IsActive;
                faqItem.UpdatedAt = DateTime.Now;

                _db.FaqItem.Update(faqItem);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("FAQ 수정 중 오류가 발생했습니다.");
                }

                string message = "FAQ가 정상적으로 수정되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return Redirect($"/Page/Faq/Item/{request.ID}/Edit");
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return await Edit(request.ID);
            }
        }

        [HttpGet("Item/{id}/Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                if (id <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                var faqItem = await _db.FaqItem.FindAsync(id);
                if (faqItem == null)
                {
                    throw new Exception("FAQ 정보를 찾을 수 없습니다.");
                }

                _db.FaqItem.Remove(faqItem);

                int affectedRows = await _db.SaveChangesAsync();
                if (affectedRows <= 0)
                {
                    throw new Exception("FAQ 삭제 중 오류가 발생했습니다.");
                }

                string message = "FAQ가 정상적으로 삭제되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect("/Page/Faq/Item");
        }

        [HttpPost("Item/Delete")]
        public async Task<IActionResult> Delete([FromForm] int[] ids)
        {
            try
            {
                if (ids == null || ids.Length <= 0)
                {
                    throw new Exception("유효하지 않은 접근입니다.");
                }

                foreach (var id in ids) 
                {
                    var faqItem = await _db.FaqItem.FindAsync(id);
                    if (faqItem == null)
                    {
                        throw new Exception("FAQ 정보를 찾을 수 없습니다.");
                    }

                    _db.FaqItem.Remove(faqItem);

                    int affectedRows = await _db.SaveChangesAsync();
                    if (affectedRows <= 0)
                    {
                        throw new Exception($"{id}번호의 FAQ 삭제 중 오류가 발생했습니다.");
                    }
                }

                string message = "FAQ가 정상적으로 삭제되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect("/Page/Faq/Item");
        }
    }
}
