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

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

        public PositionController(ILogger<PositionController> 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("Banner")]
        public IActionResult Index()
        {
            ViewBag.BannerPositions = _db.BannerPosition.Include(c => c.BannerItem).ToList();

            return View(_ViewPath);
        }

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

            try
            {
                if (request == null || !request.Any())
                {
                    // 전체 삭제
                    var bannerPositions = await _db.BannerPosition.ToListAsync();
                    if (bannerPositions.Any())
                    {
                        _db.BannerPosition.RemoveRange(bannerPositions);
                        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.BannerPosition.Select(c => c.ID).ToListAsync(); // 데이터베이스에 존재하는 ID 목록
                var idsToDelete = existingIds.Except(requestIds).ToList(); // 삭제 대상 ID: 요청 데이터에 없는 항목

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

                foreach (var row in request)
                {
                    // 중복 확인
                    if (await _db.BannerPosition.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.BannerPosition.AddAsync(row);
                    }
                    else
                    {
                        var existing = await _db.BannerPosition.FirstOrDefaultAsync(c => c.ID == row.ID);
                        if (existing == null)
                        {
                            throw new Exception($"ID {row.ID}에 해당하는 데이터가 없습니다.");
                        }

                        existing.Code = row.Code;
                        existing.Subject = row.Subject;
                        existing.IsActive = row.IsActive;
                        existing.UpdatedAt = DateTime.Now;

                        _db.BannerPosition.Update(existing);
                    }
                }

                await transaction.CommitAsync();

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

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

                ViewBag.BannerPositions = _db.BannerPosition.Include(c => c.BannerItem).ToList();

                return View(_ViewPath, request);
            }
        }
    }
}
