using Microsoft.AspNetCore.Mvc;
using Meezi.Core.Interfaces;
using Meezi.Admin.API.Services;
using Meezi.Shared;

namespace Meezi.Admin.API.Controllers;

[Route("api/admin/website")]
public class AdminWebsiteController(IAdminWebsiteService websiteAdmin) : AdminApiControllerBase
{
    // ── Blog posts ────────────────────────────────────────────────────────

    [HttpGet("posts")]
    public async Task<IActionResult> ListPosts(
        [FromQuery] int page = 1,
        [FromQuery] int limit = 20,
        [FromQuery] bool? published = null,
        CancellationToken ct = default)
    {
        var result = await websiteAdmin.ListPostsAsync(page, limit, published, ct);
        return Ok(new ApiResponse<object>(true, result));
    }

    [HttpGet("posts/{id}")]
    public async Task<IActionResult> GetPost(string id, CancellationToken ct = default)
    {
        var post = await websiteAdmin.GetPostAsync(id, ct);
        if (post is null) return NotFound(new ApiResponse<object>(false, null,
            new ApiError("NOT_FOUND", "Post not found.")));
        return Ok(new ApiResponse<object>(true, post));
    }

    [HttpPost("posts")]
    public async Task<IActionResult> CreatePost([FromBody] UpsertPostRequest req, CancellationToken ct = default)
    {
        var post = await websiteAdmin.CreatePostAsync(req, ct);
        return Ok(new ApiResponse<object>(true, post));
    }

    [HttpPut("posts/{id}")]
    public async Task<IActionResult> UpdatePost(string id,
        [FromBody] UpsertPostRequest req, CancellationToken ct = default)
    {
        var post = await websiteAdmin.UpdatePostAsync(id, req, ct);
        if (post is null) return NotFound(new ApiResponse<object>(false, null,
            new ApiError("NOT_FOUND", "Post not found.")));
        return Ok(new ApiResponse<object>(true, post));
    }

    [HttpDelete("posts/{id}")]
    public async Task<IActionResult> DeletePost(string id, CancellationToken ct = default)
    {
        await websiteAdmin.DeletePostAsync(id, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    [HttpPatch("posts/{id}/publish")]
    public async Task<IActionResult> PublishPost(string id, CancellationToken ct = default)
    {
        await websiteAdmin.SetPublishedAsync(id, true, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    [HttpPatch("posts/{id}/unpublish")]
    public async Task<IActionResult> UnpublishPost(string id, CancellationToken ct = default)
    {
        await websiteAdmin.SetPublishedAsync(id, false, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    // ── Comments ──────────────────────────────────────────────────────────

    [HttpGet("comments")]
    public async Task<IActionResult> ListComments(
        [FromQuery] bool? approved = null,
        [FromQuery] int page = 1,
        [FromQuery] int limit = 30,
        CancellationToken ct = default)
    {
        var result = await websiteAdmin.ListCommentsAsync(approved, page, limit, ct);
        return Ok(new ApiResponse<object>(true, result));
    }

    [HttpPatch("comments/{id}/approve")]
    public async Task<IActionResult> ApproveComment(string id, CancellationToken ct = default)
    {
        await websiteAdmin.SetCommentApprovedAsync(id, true, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    [HttpPatch("comments/{id}/reject")]
    public async Task<IActionResult> RejectComment(string id, CancellationToken ct = default)
    {
        await websiteAdmin.SetCommentApprovedAsync(id, false, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    [HttpDelete("comments/{id}")]
    public async Task<IActionResult> DeleteComment(string id, CancellationToken ct = default)
    {
        await websiteAdmin.DeleteCommentAsync(id, ct);
        return Ok(new ApiResponse<object>(true, null));
    }

    // ── Demo requests ─────────────────────────────────────────────────────

    [HttpGet("demo-requests")]
    public async Task<IActionResult> ListDemoRequests(
        [FromQuery] string? status = null,
        [FromQuery] int page = 1,
        [FromQuery] int limit = 30,
        CancellationToken ct = default)
    {
        var result = await websiteAdmin.ListDemoRequestsAsync(status, page, limit, ct);
        return Ok(new ApiResponse<object>(true, result));
    }

    [HttpPatch("demo-requests/{id}/status")]
    public async Task<IActionResult> UpdateDemoStatus(
        string id,
        [FromBody] UpdateDemoStatusRequest req,
        CancellationToken ct = default)
    {
        await websiteAdmin.UpdateDemoStatusAsync(id, req.Status, req.AdminNotes, ct);
        return Ok(new ApiResponse<object>(true, null));
    }
}

public record UpsertPostRequest(
    string Slug,
    string TitleFa, string? TitleEn,
    string? ExcerptFa, string? ExcerptEn,
    string ContentFa, string? ContentEn,
    string? CategoryFa, string? CategoryEn,
    string? Author,
    string? TagsJson,
    string? CoverImage,
    bool IsPublished);

public record UpdateDemoStatusRequest(string Status, string? AdminNotes);
