using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using TechHelper.Services;

namespace TechHelper.Server.Controllers
{
    /// <summary>
    /// 学校管理控制器
    /// 处理学校相关的操作，如创建、更新、删除学校等
    /// </summary>
    [Route("api/school")]
    [ApiController]
    public class SchoolController : ControllerBase
    {
        private TechHelper.Services.Beta.ISchoolService _schoolService;
        private UserManager<User> _userManager;

        /// <summary>
        /// 初始化学校控制器
        /// </summary>
        /// <param name="schoolService">学校服务</param>
        /// <param name="userManager">用户管理服务</param>
        public SchoolController(TechHelper.Services.Beta.ISchoolService schoolService, UserManager<User> userManager)
        {
            _schoolService = schoolService;
            _userManager = userManager;
        }

        #region 学校基本信息CRUD操作

        /// <summary>
        /// 获取学校列表（支持搜索）
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns>学校列表</returns>
        /// <response code="200">成功获取学校列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> GetAll([FromQuery] QueryParameter query)
        {
            var result = await _schoolService.GetAllAsync(query);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 根据ID获取学校详细信息
        /// </summary>
        /// <param name="id">学校ID</param>
        /// <returns>学校详细信息</returns>
        /// <response code="200">成功获取学校信息</response>
        /// <response code="400">学校未找到或获取失败</response>
        [HttpGet("{id}")]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var result = await _schoolService.GetAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 创建新学校
        /// </summary>
        /// <param name="model">学校数据传输对象</param>
        /// <returns>创建结果</returns>
        /// <response code="200">创建成功</response>
        /// <response code="400">创建失败</response>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] SchoolDto model)
        {
            var result = await _schoolService.AddAsync(model);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 更新学校信息
        /// </summary>
        /// <param name="model">学校数据传输对象</param>
        /// <returns>更新结果</returns>
        /// <response code="200">更新成功</response>
        /// <response code="400">更新失败</response>
        [HttpPut]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Update([FromBody] SchoolDto model)
        {
            var result = await _schoolService.UpdateAsync(model);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 删除学校
        /// </summary>
        /// <param name="id">学校ID</param>
        /// <returns>删除结果</returns>
        /// <response code="200">删除成功</response>
        /// <response code="400">删除失败</response>
        [HttpDelete("{id}")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await _schoolService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        /// <summary>
        /// 根据学校名称获取学校信息
        /// </summary>
        /// <param name="schoolName">学校名称</param>
        /// <returns>学校信息</returns>
        /// <response code="200">成功获取学校信息</response>
        /// <response code="400">学校未找到或获取失败</response>
        [HttpGet("byname/{schoolName}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetByName(string schoolName)
        {
            var result = await _schoolService.GetSchoolByNameAsync(schoolName);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        #endregion
    }
}
