import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { UserRole } from '@prisma/client';
import { Type } from 'class-transformer';
import { ArrayNotEmpty, IsArray, IsBoolean, IsEnum, IsOptional, IsString, ValidateNested } from 'class-validator';
import { FeatureKey, FEATURE_KEYS } from '../feature-catalog';
import { ESTATE_ID_EXAMPLE } from '../../common/swagger-docs';

export class RoleFeatureSettingDto {
  @ApiProperty({
    enum: FEATURE_KEYS,
    example: FeatureKey.VISITOR_PASSES_CREATE,
    description: 'Feature key from GET /features/catalog.'
  })
  @IsEnum(FeatureKey)
  feature!: FeatureKey;

  @ApiProperty({ example: true, description: 'Whether the feature is enabled for the selected role and scope.' })
  @IsBoolean()
  enabled!: boolean;
}

export class UpdateRoleFeatureSettingsDto {
  @ApiPropertyOptional({
    example: ESTATE_ID_EXAMPLE,
    description:
      'Estate override scope. SUPER_ADMIN may omit for global settings. ESTATE_ADMIN may omit or use their own estateId.'
  })
  @IsOptional()
  @IsString()
  estateId?: string;

  @ApiProperty({
    type: [RoleFeatureSettingDto],
    description: 'Feature enablement updates to upsert for this role.'
  })
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => RoleFeatureSettingDto)
  settings!: RoleFeatureSettingDto[];
}

export class UpdatePlanFeatureSettingsDto {
  @ApiProperty({
    type: [RoleFeatureSettingDto],
    description: 'Feature toggles to apply to a subscription plan. Enabled=true inserts the feature; enabled=false removes it.'
  })
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => RoleFeatureSettingDto)
  settings!: RoleFeatureSettingDto[];
}

export class RoleFeatureQueryDto {
  @ApiProperty({ enum: UserRole, example: UserRole.RESIDENT, description: 'Role to inspect.' })
  @IsEnum(UserRole)
  role!: UserRole;

  @ApiPropertyOptional({ example: ESTATE_ID_EXAMPLE, description: 'Optional estate override scope.' })
  @IsOptional()
  @IsString()
  estateId?: string;
}
