using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using LittleShop.Services;
using LittleShop.DTOs;
using System.Security.Claims;

namespace LittleShop.Areas.Admin.Controllers;

[Area("Admin")]
[Authorize(AuthenticationSchemes = "Cookies", Roles = "Admin")]
public class ReviewsController : Controller
{
    private readonly IReviewService _reviewService;
    private readonly ILogger<ReviewsController> _logger;

    public ReviewsController(IReviewService reviewService, ILogger<ReviewsController> logger)
    {
        _reviewService = reviewService;
        _logger = logger;
    }

    public async Task<IActionResult> Index()
    {
        try
        {
            var pendingReviews = await _reviewService.GetPendingReviewsAsync();
            return View(pendingReviews);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error loading reviews index");
            TempData["ErrorMessage"] = "Error loading reviews";
            return View(new List<ReviewDto>());
        }
    }

    public async Task<IActionResult> Details(Guid id)
    {
        try
        {
            var review = await _reviewService.GetReviewByIdAsync(id);
            if (review == null)
            {
                TempData["ErrorMessage"] = "Review not found";
                return RedirectToAction(nameof(Index));
            }
            return View(review);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error loading review {ReviewId}", id);
            TempData["ErrorMessage"] = "Error loading review details";
            return RedirectToAction(nameof(Index));
        }
    }

    [HttpPost]
    public async Task<IActionResult> Approve(Guid id)
    {
        try
        {
            var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            if (!Guid.TryParse(userIdClaim, out var userId))
            {
                TempData["ErrorMessage"] = "Authentication error";
                return RedirectToAction(nameof(Index));
            }

            var success = await _reviewService.ApproveReviewAsync(id, userId);
            if (success)
            {
                TempData["SuccessMessage"] = "Review approved successfully";
            }
            else
            {
                TempData["ErrorMessage"] = "Failed to approve review";
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error approving review {ReviewId}", id);
            TempData["ErrorMessage"] = "Error approving review";
        }

        return RedirectToAction(nameof(Index));
    }

    [HttpPost]
    public async Task<IActionResult> Delete(Guid id)
    {
        try
        {
            var success = await _reviewService.DeleteReviewAsync(id);
            if (success)
            {
                TempData["SuccessMessage"] = "Review deleted successfully";
            }
            else
            {
                TempData["ErrorMessage"] = "Failed to delete review";
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error deleting review {ReviewId}", id);
            TempData["ErrorMessage"] = "Error deleting review";
        }

        return RedirectToAction(nameof(Index));
    }

    public async Task<IActionResult> Edit(Guid id)
    {
        try
        {
            var review = await _reviewService.GetReviewByIdAsync(id);
            if (review == null)
            {
                TempData["ErrorMessage"] = "Review not found";
                return RedirectToAction(nameof(Index));
            }

            var updateDto = new UpdateReviewDto
            {
                Rating = review.Rating,
                Title = review.Title,
                Comment = review.Comment,
                IsApproved = review.IsApproved,
                IsActive = review.IsActive
            };

            ViewBag.ReviewId = id;
            ViewBag.ProductName = review.ProductName;
            ViewBag.CustomerName = review.CustomerDisplayName;
            
            return View(updateDto);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error loading review {ReviewId} for edit", id);
            TempData["ErrorMessage"] = "Error loading review for edit";
            return RedirectToAction(nameof(Index));
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(Guid id, UpdateReviewDto updateDto)
    {
        if (!ModelState.IsValid)
        {
            var review = await _reviewService.GetReviewByIdAsync(id);
            ViewBag.ReviewId = id;
            ViewBag.ProductName = review?.ProductName ?? "";
            ViewBag.CustomerName = review?.CustomerDisplayName ?? "";
            return View(updateDto);
        }

        try
        {
            var success = await _reviewService.UpdateReviewAsync(id, updateDto);
            if (success)
            {
                TempData["SuccessMessage"] = "Review updated successfully";
                return RedirectToAction(nameof(Details), new { id });
            }
            else
            {
                TempData["ErrorMessage"] = "Review not found";
                return RedirectToAction(nameof(Index));
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error updating review {ReviewId}", id);
            TempData["ErrorMessage"] = "Error updating review";
            
            var reviewDetails = await _reviewService.GetReviewByIdAsync(id);
            ViewBag.ReviewId = id;
            ViewBag.ProductName = reviewDetails?.ProductName ?? "";
            ViewBag.CustomerName = reviewDetails?.CustomerDisplayName ?? "";
            return View(updateDto);
        }
    }
}