import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AdminGuard } from './admin.guard';
import { PrismaService } from '../../prisma/prisma.service';

@ApiTags('管理后台 - 角色权限')
@Controller('admin/settings')
@UseGuards(AuthGuard('jwt'), AdminGuard)
@ApiBearerAuth()
export class SettingsController {
  constructor(private prisma: PrismaService) {}

  @Get('roles')
  async roles() {
    return { items: await this.prisma.adminRole.findMany({ where: { status: 'ACTIVE' }, orderBy: { createdAt: 'desc' } }) };
  }

  @Post('roles')
  async createRole(@Body() body: { name: string; description?: string; permissions?: string[] }) {
    return this.prisma.adminRole.create({ data: { name: body.name, description: body.description || '', permissions: body.permissions || [] } });
  }

  @Put('roles/:id')
  async updateRole(@Param('id') id: string, @Body() body: { name?: string; description?: string; permissions?: string[] }) {
    const data: any = {};
    if (body.name) data.name = body.name;
    if (body.description !== undefined) data.description = body.description;
    if (body.permissions) data.permissions = body.permissions;
    return this.prisma.adminRole.update({ where: { id }, data });
  }

  @Delete('roles/:id')
  async deleteRole(@Param('id') id: string) {
    return this.prisma.adminRole.update({ where: { id }, data: { status: 'DISABLED' } });
  }

  @Get('admins')
  async admins() {
    return { items: await this.prisma.adminUser.findMany({ where: { status: 'ACTIVE' }, include: { role: true }, orderBy: { createdAt: 'desc' } }) };
  }

  @Post('admins')
  async createAdmin(@Body() body: { username: string; password: string; nickname?: string; roleId?: string }) {
    const bcrypt = require('bcryptjs');
    const passwordHash = await bcrypt.hash(body.password, 10);
    return this.prisma.adminUser.create({ data: { username: body.username, passwordHash, nickname: body.nickname || '', roleId: body.roleId || null } });
  }

  @Put('admins/:id')
  async updateAdmin(@Param('id') id: string, @Body() body: { nickname?: string; roleId?: string; status?: string }) {
    const data: any = {};
    if (body.nickname !== undefined) data.nickname = body.nickname;
    if (body.roleId !== undefined) data.roleId = body.roleId;
    if (body.status) data.status = body.status;
    return this.prisma.adminUser.update({ where: { id: parseInt(id) }, data });
  }

  @Delete('admins/:id')
  async deleteAdmin(@Param('id') id: string) {
    return this.prisma.adminUser.update({ where: { id: parseInt(id) }, data: { status: 'DISABLED' } });
  }

  @Get('permissions')
  permissions() {
    return {
      items: [
        { key: 'dashboard', name: '仪表盘', category: '首页' }, { key: 'users', name: '用户管理', category: '用户' },
        { key: 'courses', name: '课程管理', category: '内容' }, { key: 'prompts', name: '提示词管理', category: '内容' },
        { key: 'contents', name: '内容管理', category: '内容' }, { key: 'tools', name: '工具管理', category: '内容' },
        { key: 'orders', name: '订单管理', category: '交易' }, { key: 'enterprise', name: '企业版管理', category: '交易' },
        { key: 'comments', name: '评论审核', category: '社区' }, { key: 'analytics', name: '数据分析', category: '统计' },
        { key: 'roles', name: '角色权限', category: '设置' }, { key: 'admins', name: '管理员', category: '设置' },
        { key: 'config', name: '系统配置', category: '设置' }, { key: 'banners', name: 'Banner管理', category: '运营' },
        { key: 'notifications', name: '推送管理', category: '运营' },
      ],
    };
  }
}
