using HoneyBox.Admin.Business.Attributes;
using HoneyBox.Admin.Business.Models;
using HoneyBox.Admin.Business.Models.Dashboard;
using HoneyBox.Admin.Business.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 仪表盘控制器
/// </summary>
[Route("api/admin/business/dashboard")]
public class DashboardController : BusinessControllerBase
{
    private readonly IDashboardService _dashboardService;

    public DashboardController(IDashboardService dashboardService)
    {
        _dashboardService = dashboardService;
    }

    /// <summary>
    /// 获取仪表盘概览数据
    /// </summary>
    /// <returns>仪表盘概览</returns>
    [HttpGet]
    [BusinessPermission("dashboard:view")]
    public async Task<IActionResult> GetOverview()
    {
        var result = await _dashboardService.GetOverviewAsync();
        return Ok(result);
    }

    /// <summary>
    /// 获取广告账户列表
    /// </summary>
    /// <returns>广告账户列表</returns>
    [HttpGet("ads")]
    [BusinessPermission("dashboard:view")]
    public async Task<IActionResult> GetAdAccounts()
    {
        var result = await _dashboardService.GetAdAccountsAsync();
        return Ok(result);
    }

    /// <summary>
    /// 创建广告账户
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新广告账户ID</returns>
    [HttpPost("ads")]
    [BusinessPermission("dashboard:edit")]
    public async Task<IActionResult> CreateAdAccount([FromBody] AdAccountCreateRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.ImgUrl))
        {
            return ValidationError("广告图片URL不能为空");
        }

        var id = await _dashboardService.CreateAdAccountAsync(request);
        return Ok(new { id });
    }

    /// <summary>
    /// 删除广告账户
    /// </summary>
    /// <param name="id">广告账户ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("ads/{id}")]
    [BusinessPermission("dashboard:edit")]
    public async Task<IActionResult> DeleteAdAccount(int id)
    {
        var result = await _dashboardService.DeleteAdAccountAsync(id);
        if (!result)
        {
            return NotFoundError("广告账户不存在");
        }
        return Ok("删除成功");
    }
}
