import {
  IsArray,
  IsBoolean,
  IsDecimal,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  IsUUID,
  Min,
  MinLength,
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { BookingMembershipKind } from '@prisma/client';
import { Type } from 'class-transformer';

// ─── Membership Types ─────────────────────────────────────

export class CreateMembershipTypeDto {
  @ApiProperty({ example: 'Безлимит 30 дней' })
  @IsString()
  @MinLength(1)
  name: string;

  @ApiProperty({ enum: BookingMembershipKind, example: 'UNLIMITED' })
  @IsEnum(BookingMembershipKind)
  kind: BookingMembershipKind;

  @ApiPropertyOptional({ example: 12, description: 'Total visits (VISIT_BASED / MIXED)' })
  @IsOptional()
  @IsInt()
  @Min(1)
  totalVisits?: number;

  @ApiPropertyOptional({ example: '10.00', description: 'Total hours (HOUR_BASED / MIXED)' })
  @IsOptional()
  @IsDecimal()
  totalHours?: string;

  @ApiProperty({ example: 30, description: 'Validity period in days' })
  @IsInt()
  @Min(1)
  validityDays: number;

  @ApiProperty({ example: '5000.00' })
  @IsDecimal()
  price: string;

  @ApiPropertyOptional({
    example: ['serviceId1', 'serviceId2'],
    description: 'Service IDs covered by this membership. Empty = all services.',
  })
  @IsOptional()
  @IsArray()
  @IsUUID('4', { each: true })
  serviceIds?: string[];

  @ApiPropertyOptional({ example: true, default: true })
  @IsOptional()
  @IsBoolean()
  autoActivate?: boolean;
}

export class UpdateMembershipTypeDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MinLength(1)
  name?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Min(1)
  totalVisits?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsDecimal()
  totalHours?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Min(1)
  validityDays?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsDecimal()
  price?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsArray()
  @IsUUID('4', { each: true })
  serviceIds?: string[];

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  autoActivate?: boolean;

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

// ─── Client Memberships ────────────────────────────────────

export class IssueMembershipDto {
  @ApiProperty({ description: 'Client ID' })
  @IsUUID()
  clientId: string;

  @ApiProperty({ description: 'Membership type ID' })
  @IsUUID()
  typeId: string;

  @ApiPropertyOptional({ description: 'Activate immediately (overrides autoActivate setting)' })
  @IsOptional()
  @IsBoolean()
  activateNow?: boolean;
}

export class FreezeMembershipDto {
  @ApiProperty({ example: 14, description: 'Number of days to freeze' })
  @IsInt()
  @Min(1)
  days: number;
}

export class FindClientMembershipsQuery {
  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  clientId?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  @Type(() => Boolean)
  activeOnly?: boolean;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  cursor?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Min(1)
  @Type(() => Number)
  limit?: number;
}
