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

namespace bitforum.Controllers.Page.Faq
{
    [Authorize]
    [Route("Page")]
    public class CategoryController : Controller
    {
        private readonly ILogger<CategoryController> _logger;
        private readonly DefaultDbContext _db;
        private readonly string _ViewPath = "~/Views/Page/Faq/Category/Index.cshtml";

        public CategoryController(ILogger<CategoryController> 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 });
        }

        [HttpGet("Faq")]
        public IActionResult Index()
        {
            ViewBag.FaqCategories = _db.FaqCategory.Include(c => c.FaqItem).OrderBy(c => c.Order).ToList();

            return View(_ViewPath);
        }

        [HttpPost("Faq")]
        public async Task<IActionResult> Save([FromForm] List<FaqCategory> request)
        {
            using var transaction = await _db.Database.BeginTransactionAsync();

            try
            {
                if (request == null || !request.Any())
                {
                    // 전체 삭제
                    var faqCategories = await _db.FaqCategory.ToListAsync();
                    if (faqCategories.Any())
                    {
                        _db.FaqCategory.RemoveRange(faqCategories);
                        await _db.SaveChangesAsync();
                    }

                    await transaction.CommitAsync();
                    return RedirectToAction("Index");
                }

                if (!ModelState.IsValid)
                {
                    throw new Exception("유효성 검사에 실패하였습니다.");
                }
                
                var requestIds = request.Select(x => x.ID).ToList(); // 요청 데이터의 ID 목록
                var existingIds = await _db.FaqCategory.Select(c => c.ID).ToListAsync(); // 데이터베이스에 존재하는 ID 목록
                var idsToDelete = existingIds.Except(requestIds).ToList(); // 삭제 대상 ID: 요청 데이터에 없는 항목

                // 삭제 대상 항목 제거
                if (idsToDelete.Any())
                {
                    var selectedRows = await _db.FaqCategory.Where(c => idsToDelete.Contains(c.ID) && !c.FaqItem.Any()).ToListAsync();
                    _db.FaqCategory.RemoveRange(selectedRows);
                }

                foreach (var row in request)
                {
                    // 중복 확인
                    if (await _db.FaqCategory.AnyAsync(c => c.Code == row.Code && c.ID != row.ID))
                    {
                        throw new Exception($"이미 존재하는 분류 주소입니다: {row.Code}");
                    }

                    if (row.ID == 0)
                    {
                        row.UpdatedAt = null;
                        row.CreatedAt = DateTime.Now;
                        await _db.FaqCategory.AddAsync(row);
                    }
                    else
                    {
                        var existing = await _db.FaqCategory.FirstOrDefaultAsync(c => c.ID == row.ID);
                        if (existing == null)
                        {
                            throw new Exception($"ID {row.ID}에 해당하는 데이터가 없습니다.");
                        }

                        // 기존 엔터티 업데이트
                        existing.Code = row.Code;
                        existing.Subject = row.Subject;
                        existing.Order = row.Order;
                        existing.IsActive = row.IsActive;
                        existing.UpdatedAt = DateTime.Now;

                        _db.FaqCategory.Update(existing);
                    }
                }

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

                string message = "FAQ 분류가 정상적으로 저장되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                await transaction.RollbackAsync();
                _logger.LogError(e, e.Message);
                TempData["ErrorMessages"] = e.Message;
                return View(_ViewPath, request);
            }
        }
    }
}
