using System;
using Application;
using Microsoft.AspNetCore.Mvc;

namespace Website.Controllers
{
  public class CommentsController : BaseController
  {
    private readonly CommentService _commentService;
    private readonly LinkService _linkService;

    public CommentsController(CommentService commentService, LinkService linkService)
    {
      _commentService = commentService;
      _linkService = linkService;
    }

    [HttpGet("links/{linkId:guid}/comment")]
    public IActionResult Create(Guid linkId)
    {
      ViewData["Url"] = _linkService.GetLinkById(linkId).Url;

      return View();
    }

    [HttpPost("links/{linkId:guid}/comment")]
    [ValidateAntiForgeryToken]
    public IActionResult Create(PublishCommentCommand cmd)
    {
      if (!ModelState.IsValid)
      {
        ViewData["Url"] = _linkService.GetLinkById(cmd.LinkId).Url;

        return View(cmd);
      }

      _commentService.PublishComment(cmd);

      Success("Your comment was published!");

      // return Redirect($"/links/detail/{cmd.LinkId}");

      return RedirectToAction(nameof(LinksController.Show), "Links", new { linkId = cmd.LinkId });
    }
  }
}