import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { CurrentUser, JwtPayload } from '../../../common/decorators/current-user.decorator';
import { RequireModule } from '../../../common/decorators/require-module.decorator';
import { ModuleGuard } from '../../../common/guards/module.guard';
import { Roles, UserRole } from '../../../common/decorators/roles.decorator';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { MembershipsService } from '../services/memberships.service';
import { ClientMembershipsService } from '../services/client-memberships.service';
import {
  CreateMembershipTypeDto,
  UpdateMembershipTypeDto,
  IssueMembershipDto,
  FreezeMembershipDto,
  FindClientMembershipsQuery,
} from '../dto/membership.dto';

// ═══════════════════════════════════════════════════════════
// Membership Types
// ═══════════════════════════════════════════════════════════

@ApiTags('Booking / Membership Types')
@ApiBearerAuth()
@Controller('booking/memberships')
@UseGuards(JwtAuthGuard, ModuleGuard, RolesGuard)
@RequireModule('booking')
export class BookingMembershipTypesController {
  constructor(private readonly memberships: MembershipsService) {}

  @Get()
  @ApiOperation({ summary: 'List membership types' })
  @ApiQuery({ name: 'activeOnly', required: false, type: Boolean })
  async findAll(@CurrentUser() user: JwtPayload, @Query('activeOnly') activeOnly?: string) {
    return this.memberships.findAll(user.clubId, activeOnly === 'true');
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get membership type by ID' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.memberships.findById(id);
  }

  @Post()
  @ApiOperation({ summary: 'Create membership type' })
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async create(@CurrentUser() user: JwtPayload, @Body() dto: CreateMembershipTypeDto) {
    return this.memberships.create(user.clubId, dto);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update membership type' })
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateMembershipTypeDto) {
    return this.memberships.update(id, dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete membership type' })
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async delete(@Param('id', ParseUUIDPipe) id: string) {
    return this.memberships.delete(id);
  }
}

// ═══════════════════════════════════════════════════════════
// Client Memberships
// ═══════════════════════════════════════════════════════════

@ApiTags('Booking / Client Memberships')
@ApiBearerAuth()
@Controller('booking/client-memberships')
@UseGuards(JwtAuthGuard, ModuleGuard, RolesGuard)
@RequireModule('booking')
export class BookingClientMembershipsController {
  constructor(private readonly clientMemberships: ClientMembershipsService) {}

  @Get()
  @ApiOperation({ summary: 'List client memberships' })
  async findAll(@CurrentUser() user: JwtPayload, @Query() query: FindClientMembershipsQuery) {
    return this.clientMemberships.findAll(user.clubId, query);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get client membership by ID' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.clientMemberships.findById(id);
  }

  @Post()
  @ApiOperation({ summary: 'Issue membership to client' })
  @Roles(UserRole.MANAGER, UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async issue(@CurrentUser() user: JwtPayload, @Body() dto: IssueMembershipDto) {
    return this.clientMemberships.issue(user.clubId, dto, user.sub);
  }

  @Post(':id/freeze')
  @ApiOperation({ summary: 'Freeze membership' })
  @Roles(UserRole.MANAGER, UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async freeze(@Param('id', ParseUUIDPipe) id: string, @Body() dto: FreezeMembershipDto) {
    return this.clientMemberships.freeze(id, dto);
  }

  @Post(':id/unfreeze')
  @ApiOperation({ summary: 'Unfreeze membership' })
  @Roles(UserRole.MANAGER, UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  async unfreeze(@Param('id', ParseUUIDPipe) id: string) {
    return this.clientMemberships.unfreeze(id);
  }

  @Get('client/:clientId/valid')
  @ApiOperation({ summary: 'Get valid memberships for client + service' })
  @ApiQuery({ name: 'serviceId', type: String })
  async findValid(
    @CurrentUser() user: JwtPayload,
    @Param('clientId', ParseUUIDPipe) clientId: string,
    @Query('serviceId') serviceId: string,
  ) {
    return this.clientMemberships.findValidForClient(user.clubId, clientId, serviceId);
  }
}
