import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean, IsEmail, IsIn, IsInt, IsOptional, IsString, Matches, Max, MaxLength, Min, MinLength } from 'class-validator';
import { STAFF_ROLES, UserType } from 'src/user/schema/users.schema';

const trim = ({ value }: { value: unknown }) => (typeof value === 'string' ? value.trim() : value);
const lowerTrim = ({ value }: { value: unknown }) => (typeof value === 'string' ? value.trim().toLowerCase() : value);

export enum StaffStatus {
    ACTIVE = 'ACTIVE',
    INACTIVE = 'INACTIVE',
    INVITED = 'INVITED'
}

export class CreateStaffDto {
    @ApiProperty({ example: 'Ananya Rao' })
    @IsString()
    @MinLength(2)
    @MaxLength(80)
    @Transform(trim)
    name: string;

    @ApiProperty({ example: '9876543210', description: 'Mobile number, digits only' })
    @Matches(/^\d{7,15}$/)
    @Transform(trim)
    phone_no: string;

    @ApiProperty({ required: false, default: '+91' })
    @IsOptional()
    @Matches(/^\+\d{1,4}$/)
    @Transform(trim)
    country_code?: string;

    @ApiProperty({ example: 'staff@readybroker.in', description: 'The invite link is sent here' })
    @IsEmail()
    @Transform(lowerTrim)
    email: string;

    @ApiProperty({ enum: STAFF_ROLES, description: 'POLICY_STAFF works leads and policies; ADMIN_STAFF is a Sub-Admin' })
    @IsIn(STAFF_ROLES)
    user_type: UserType;
}

export class UpdateStaffDto {
    @ApiProperty({ required: false })
    @IsOptional()
    @IsString()
    @MinLength(2)
    @MaxLength(80)
    @Transform(trim)
    name?: string;

    @ApiProperty({ required: false })
    @IsOptional()
    @Matches(/^\d{7,15}$/)
    @Transform(trim)
    phone_no?: string;

    @ApiProperty({ required: false })
    @IsOptional()
    @Matches(/^\+\d{1,4}$/)
    @Transform(trim)
    country_code?: string;

    @ApiProperty({ required: false, enum: STAFF_ROLES, description: 'Change role (Master Admin only when it involves ADMIN_STAFF)' })
    @IsOptional()
    @IsIn(STAFF_ROLES)
    user_type?: UserType;

    @ApiProperty({ required: false, description: 'false deactivates the account and logs the user out' })
    @IsOptional()
    @IsBoolean()
    is_active?: boolean;
}

export class StaffListDto {
    @ApiProperty({ required: false, default: 1 })
    @IsOptional()
    @Transform(({ value }) => Number(value))
    @IsInt()
    @Min(1)
    page: number = 1;

    @ApiProperty({ required: false, default: 10 })
    @IsOptional()
    @Transform(({ value }) => Number(value))
    @IsInt()
    @Min(1)
    @Max(100)
    limit: number = 10;

    @ApiProperty({ required: false, description: 'Search by name, email or mobile' })
    @IsOptional()
    @IsString()
    search?: string;

    @ApiProperty({ required: false, enum: STAFF_ROLES })
    @IsOptional()
    @IsIn(STAFF_ROLES)
    user_type?: UserType;

    @ApiProperty({ required: false, enum: StaffStatus })
    @IsOptional()
    @IsIn(Object.values(StaffStatus))
    status?: StaffStatus;
}
