import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsBoolean, IsEnum, IsOptional, IsString, IsUUID, IsNumber, IsArray } from 'class-validator';
import { RoleType } from 'common/enum/permission-state.enum';
import { PermissionWithStateDto, SubPermissionWithStateDto } from 'modules/role-permission/permissions/dto/permission-state.dto';
import { RoleEntity } from '../entities/role.entity';

export class RoleResponseDto {
  @ApiProperty({
    description: 'Unique identifier for the role',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @IsUUID()
  id: string;

  @ApiProperty({
    description: 'Name of the role',
    example: 'Admin',
  })
  @IsString()
  name: string;

  @ApiProperty({
    description: 'Type of the role',
    enum: RoleType,
    example: RoleType.CLIENT_ROLE,
  })
  @IsEnum(RoleType)
  roleType: RoleType;

  @ApiProperty({
    description: 'Indicates if this is a custom role',
    example: true,
  })
  @IsBoolean()
  isCustom: boolean;

  @ApiProperty({
    description: 'Priority of the role (lower number = higher priority)',
    example: 1,
  })
  @IsNumber()
  priority: number;

  @ApiPropertyOptional({
    description: 'ID of the client if role is client-specific',
    example: '223e4567-e89b-12d3-a456-426614174111',
  })
  @IsUUID()
  @IsOptional()
  clientId?: string;

  @ApiProperty({
    description: 'List of permissions with their override state',
    type: [PermissionWithStateDto],
  })
  @IsArray()
  permissions: PermissionWithStateDto[];

  @ApiProperty({
    description: 'List of sub-permissions with their override state',
    type: [SubPermissionWithStateDto],
  })
  @IsArray()
  subPermissions: SubPermissionWithStateDto[];

  @ApiPropertyOptional({
    description: 'Number of users linked to this role',
    example: 10,
  })
  @IsNumber()
  @IsOptional()
  userCount?: number;

  @ApiProperty({
    description: 'Date when the role was created',
    example: '2023-08-15T00:00:00.000Z',
  })
  createdAt: Date;

  @ApiProperty({
    description: 'Date when the role was last updated',
    example: '2023-08-20T00:00:00.000Z',
  })
  updatedAt: Date;
}

export function mapRoleResponseDto(role: RoleEntity): RoleResponseDto {
  const roleResponse = new RoleResponseDto();
  roleResponse.id = role.id;
  roleResponse.name = role.name;
  roleResponse.roleType = role.roleType;
  roleResponse.isCustom = role.isCustom;
  roleResponse.priority = role.priority;
  roleResponse.clientId = role.client?.id; // Assuming client is optional
  roleResponse.permissions = role.permissionStates.map(ps => ({
    id: ps.permission.id,
    name: ps.permission.name,
    displayName: ps.permission.displayName,
    description: ps.permission.description,
    isActive: ps.permission.isActive,
    state: ps.permissionState,
  }));
  roleResponse.subPermissions = []; // Assuming sub-permissions are not included in the RoleEntity
  roleResponse.userCount = role.users?.length || 0; // Count of users linked to this role
  roleResponse.createdAt = role.createdAt;
  roleResponse.updatedAt = role.updatedAt;

  return roleResponse;
}