using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using bitforum.Models;
using bitforum.Models.Page;
using bitforum.Constants;
using bitforum.Helpers;
using bitforum.Services;
using bitforum.Repository;

namespace bitforum.Controllers.Page
{
    [Authorize]
    [Route("Page")]
    public class DocumentController : Controller
    {
        private readonly ILogger<DocumentController> _logger;
        private readonly IFileUploadService _fileUploadService;
        private readonly IRedisRepository _redisRepository;
        private readonly DefaultDbContext _db;
        private readonly string _IndexViewPath = "~/Views/Page/Document/Index.cshtml";
        private readonly string _WriteViewPath = "~/Views/Page/Document/Write.cshtml";
        private readonly string _EditViewPath = "~/Views/Page/Document/Edit.cshtml";

        public DocumentController(ILogger<DocumentController> logger, IFileUploadService fileUploadService, IRedisRepository redisRepository, DefaultDbContext db)
        {
            _logger = logger;
            _fileUploadService = fileUploadService;
            _redisRepository = redisRepository;
            _db = db;
        }

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

        [HttpGet("Document")]
        public IActionResult Index()
        {
            var domain = Setting.AppConfig.AppName;
            var documents = _db.Document.OrderByDescending(c => c.ID).ToList();
            var data = new List<object>();

            if (documents.Count > 0)
            {
                foreach (var row in documents)
                {
                    var entry = new
                    {
                        row.ID,
                        Link = $"{domain }/docs/{row.Code}",
                        row.Code,
                        row.Subject,
                        row.Content,
                        Views = row.Views.ToString("N0"),
                        IsActive = (row.IsActive ? 'Y' : 'N'),
                        UpdatedAt = row.UpdatedAt.GetDateAt(),
                        CreatedAt = row.CreatedAt.GetDateAt(),
                        EditURL = $"/Page/Document/{row.ID}/Edit",
                        DeleteURL = $"/Page/Document/{row.ID}/Delete"
                    };

                    data.Add(entry);
                }
            }

            ViewBag.Data = data;
            ViewBag.Total = (data?.Count ?? 0);

            return View(_IndexViewPath);
        }

        [HttpGet("Document/Write")]
        public IActionResult Write()
        {
            ViewBag.siteURL = Setting.AppConfig.AppName;
            return View(_WriteViewPath);
        }

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

                // 중복확인
                if (await _db.Document.AnyAsync(c => c.Code == request.Code))
                {
                    throw new Exception("이미 존재하는 주소입니다.");
                }


                request.Content = _fileUploadService.UploadEditorAsync(request.Content, null, UploadFolder.Document).Result;
                request.UpdatedAt = null;
                request.CreatedAt = DateTime.UtcNow;

                _db.Document.Add(request);

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

                await _redisRepository.SetObjectAsync($"{RedisConst.DocumentKey}-{request.Code}", request, RedisConst.CacheExpiration);

                string message = "문서가 등록되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
                _logger.LogError(e, e.Message);
                return View(_WriteViewPath, request);
            }
        }

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

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

                ViewBag.siteURL = Setting.AppConfig.AppName;
                return View(_EditViewPath, document);
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
                _logger.LogError(e, e.Message);
                return Index();
            }
        }

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

                // 중복확인
                if (await _db.Document.AnyAsync(c => c.Code == request.Code && c.ID != request.ID))
                {
                    throw new Exception("이미 존재하는 Code 주소입니다.");
                }

                // 기존 문서 조회
                var document = await _db.Document.FindAsync(request.ID);
                if (document is null)
                {
                    throw new Exception("사용자 정보를 찾을 수 없습니다.");
                }

                document.IsActive = request.IsActive;
                document.Code = request.Code;
                document.Subject = request.Subject;
                document.Content = _fileUploadService.UploadEditorAsync(request.Content, document.Content, UploadFolder.Document).Result;
                document.UpdatedAt = DateTime.UtcNow;

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

                await _redisRepository.SetObjectAsync($"{RedisConst.DocumentKey}-{request.Code}", document, RedisConst.CacheExpiration);

                string message = $"{document.ID}번 문서가 수정되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
                return await Edit(request.ID);
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
                _logger.LogError(e, e.Message);
                return View(_EditViewPath, request);
            }
        }

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

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

                _db.Document.Remove(document);

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

                await _fileUploadService.CleanUpEditorImagesAsync(document.Content);
                await _redisRepository.DeleteAsync($"{RedisConst.DocumentKey}-{document.Code}");

                string message = "문서가 삭제되었습니다.";
                TempData["SuccessMessage"] = message;
                _logger.LogInformation(message);
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
                _logger.LogError(e, e.Message);
            }
            
            return Index();
        }
    }
}
