import { Controller, Get, Post, Patch, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';

@ApiTags('admin-release')
@ApiBearerAuth()
@Controller('admin-api/release')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
export class ReleaseController {
  constructor(private readonly prisma: PrismaService) {}

  // ═══ Changelogs ═══

  @Get('changelogs')
  @ApiOperation({ summary: '版本日志列表' })
  async listChangelogs() {
    return this.prisma.appChangelog.findMany({ orderBy: { createdAt: 'desc' } });
  }

  @Post('changelogs')
  @ApiOperation({ summary: '创建版本日志' })
  async createChangelog(@Body() dto: { version: string; title: string; content: string; platform?: string }) {
    return this.prisma.appChangelog.create({ data: { ...dto, platform: dto.platform || 'ios' } });
  }

  @Patch('changelogs/:id')
  @ApiOperation({ summary: '更新版本日志' })
  async updateChangelog(@Param('id') id: string, @Body() dto: Record<string, any>) {
    return this.prisma.appChangelog.update({ where: { id }, data: dto });
  }

  @Delete('changelogs/:id')
  @ApiOperation({ summary: '删除版本日志' })
  async deleteChangelog(@Param('id') id: string) {
    await this.prisma.appChangelog.delete({ where: { id } });
    return { ok: true };
  }

  // ═══ Decision Records (ADR) ═══

  @Get('decisions')
  @ApiOperation({ summary: '决策记录列表' })
  async listDecisions() {
    return this.prisma.decisionRecord.findMany({ orderBy: { createdAt: 'desc' } });
  }

  @Post('decisions')
  @ApiOperation({ summary: '创建决策记录' })
  async createDecision(@Body() dto: { title: string; context?: string; decision?: string; rationale?: string }) {
    return this.prisma.decisionRecord.create({ data: dto });
  }

  @Patch('decisions/:id')
  @ApiOperation({ summary: '更新决策记录' })
  async updateDecision(@Param('id') id: string, @Body() dto: Record<string, any>) {
    return this.prisma.decisionRecord.update({ where: { id }, data: dto });
  }

  @Delete('decisions/:id')
  @ApiOperation({ summary: '删除决策记录' })
  async deleteDecision(@Param('id') id: string) {
    await this.prisma.decisionRecord.delete({ where: { id } });
    return { ok: true };
  }

  // ═══ Release Checklist ═══

  @Get('checklists/:version')
  @ApiOperation({ summary: '版本回归清单' })
  async listChecklist(@Param('version') version: string) {
    return this.prisma.releaseChecklist.findMany({
      where: { version },
      orderBy: { sortOrder: 'asc' },
    });
  }

  @Post('checklists')
  @ApiOperation({ summary: '添加检查项' })
  async addChecklistItem(@Body() dto: { version: string; item: string; sortOrder?: number }) {
    return this.prisma.releaseChecklist.create({ data: dto });
  }

  @Patch('checklists/:id')
  @ApiOperation({ summary: '更新检查项（标记完成）' })
  async toggleChecklist(@Param('id') id: string, @Body() dto: { checked?: boolean; item?: string }) {
    return this.prisma.releaseChecklist.update({ where: { id }, data: dto });
  }
}
