import { ApiProperty } from "@nestjs/swagger";
import { IsArray, IsEnum, IsMongoId, IsNotEmpty, IsOptional, IsString, ValidateIf } from "class-validator";

export class RequestDto {
    user_data: any;
}

export enum muteNotificationType {
    HOUR = "HOUR", WEEK = "WEEK", ALWAYS = "ALWAYS"
}

export enum GroupOperations {
    EXIT_GROUP = "EXIT_GROUP",
    EXIT_GROUP_AND_DELETE_FOR_ME = "EXIT_GROUP_AND_DELETE_FOR_ME",
    CLEAR_CHAT = "CLEAR_CHAT",
    LOCK_CONNECTION = "LOCK_CONNECTION",
    UNLOCK_CONNECTION = "UNLOCK_CONNECTION",
    PIN_CONNECTIONS = "PIN_CONNECTIONS",
    UNPIN_CONNECTIONS = "UNPIN_CONNECTIONS",
    ARCHIVE_CONNECTIONS = "ARCHIVE_CONNECTIONS",
    UNARCHIVE_CONNECTIONS = "UNARCHIVE_CONNECTIONS",
    DELETE_CONNECTIONS = "DELETE_CONNECTIONS",
    DELETE_CONNECTIONS_WITH_CHAT = "DELETE_CONNECTIONS_WITH_CHAT",
    DELETE_MESSAGES_FOR_ME = "DELETE_MESSAGES_FOR_ME",
    DELETE_MESSAGES_FOR_EVERYONE = "DELETE_MESSAGES_FOR_EVERYONE",
    REMOVE_MEMBER_FROM_GROUP = "REMOVE_MEMBER_FROM_GROUP",
    ADD_MEMBER_TO_GROUP = "ADD_MEMBER_TO_GROUP",
    BLOCK_OR_UNBLOCK_USERS = "BLOCK_OR_UNBLOCK_USERS",
    STARRED_MESSAGES = "STARRED_MESSAGES",
    UNSTARRED_MESSAGES = "UNSTARRED_MESSAGES",
    UNSTARRED_ALL_MESSAGES = "UNSTARRED_ALL_MESSAGES",
    PIN_MESSAGES = "PIN_MESSAGES",
    UNPIN_MESSAGES = "UNPIN_MESSAGES",
}

export enum ConnectionTypes {
    NORMAL = "NORMAL",
    GROUP = "GROUP",
}

export enum chatTypes {
    NORMAL = "NORMAL",
    ARCHIVE = "ARCHIVE",
    LOCKED = "LOCKED",
}
export class CreateConnection {
    @ApiProperty({ required: true })
    @IsNotEmpty()
    @IsString()
    sent_to: string;
}

export class SendMessageFromPushDto {
    @ApiProperty()
    connection_id: string;
  
    @ApiProperty()
    message: any;
  
    @ApiProperty()
    sent_to: string;
  
    @ApiProperty()
    sent_by: string;
  
    @ApiProperty()
    type: string;
  
    @ApiProperty()
    media_url: string;
    
    @ApiProperty()
    message_type: string;
  
    @ApiProperty()
    message_id: string;
  
    @ApiProperty()
    lat: string;
  
    @ApiProperty()
    long: string;
  
    @ApiProperty()
    live_end_at: string;
}

export class paginationDto {
    @ApiProperty({ description: "search", default: null, required: false })
    @IsOptional()
    @IsString()
    search?: string;

    @ApiProperty({ description: "pagination", required: false })
    @IsOptional()
    pagination?: number;

    @ApiProperty({ description: "limit", required: false })
    @IsOptional()
    limit?: number;
}


export class ConnectionDto {
    @ApiProperty({ description: "type like archive locked chat", default: chatTypes.NORMAL, required: false, enum: chatTypes })
    @IsString()
    @IsOptional()
    @IsEnum(chatTypes)
    type: string;
    
    @ApiProperty({ description: "type like normal or group", default: ConnectionTypes.NORMAL, required: false, enum: ConnectionTypes })
    @IsString()
    @IsOptional()
    @IsEnum(ConnectionTypes)
    conn_type: string;

    @ApiProperty({ description: "search", default: null, required: false })
    @IsOptional()
    @IsString()
    search?: string;

    @ApiProperty({ description: "pagination", required: false })
    @IsOptional()
    pagination?: number;

    @ApiProperty({ description: "limit", required: false })
    @IsOptional()
    limit?: number;
}

export class chatHistory {
    @ApiProperty({})
    @IsNotEmpty()
    @IsString()
    connection_id?: string;

    @ApiProperty({ required: false })
    @IsOptional()
    page?: string;

    @ApiProperty({ required: false })
    @IsOptional()
    limit?: string;
}

export class MuteNotificationType {
    @ApiProperty({})
    @IsString()
    @IsOptional()
    connection_id?: string;

    @ApiProperty({ description: muteNotificationType.ALWAYS, required: false })
    @IsOptional()
    @IsEnum(muteNotificationType)
    type?: muteNotificationType;

    @ApiProperty({ description: "no_of_message", required: false })
    @IsOptional()
    no_of_days_or_hours?: number;
}

export class BlockUserDto {
    @ApiProperty({})
    @IsString()
    user_id: string;
}

export class CreateGroupDto {
    @ApiProperty({ required: true })
    @IsNotEmpty()
    @IsString()
    name: string;

    @ApiProperty({ required: true })
    @IsString()
    image: string;

    @ApiProperty({ type: [String], required: true })
    @IsArray()
    @IsMongoId({ each: true })
    members: string[];
}

export class GroupOperationsDto {
    @ApiProperty({ required: true, enum: GroupOperations })
    @IsNotEmpty()
    @IsString()
    @IsEnum(GroupOperations)
    operation_type: string;

    // CONDITION: Only required when operation = REMOVE_MEMBER or ADD_MEMBER
    @ApiProperty({ type: [String], required: false })
    @ValidateIf((o: GroupOperationsDto) =>
        o.operation_type === GroupOperations.REMOVE_MEMBER_FROM_GROUP ||
        o.operation_type === GroupOperations.ADD_MEMBER_TO_GROUP
    )
    @IsArray()
    @IsMongoId({ each: true })
    @IsNotEmpty()
    _ids: string[];

    // CONDITION: Required for REMOVE_MEMBER, ADD_MEMBER, PIN_MESSAGES, UNPIN_MESSAGES (used as connection_id)
    @ApiProperty({ type: String, required: false, description: "Connection ID" })
    @ValidateIf(o =>
        o.operation_type === GroupOperations.REMOVE_MEMBER_FROM_GROUP ||
        o.operation_type === GroupOperations.ADD_MEMBER_TO_GROUP ||
        o.operation_type === GroupOperations.PIN_MESSAGES ||
        o.operation_type === GroupOperations.UNPIN_MESSAGES
    )
    @IsMongoId()
    @IsNotEmpty()
    _id: string;

    // CONDITION: Only required for PIN_MESSAGES and UNPIN_MESSAGES operations
    @ApiProperty({ type: String, required: false, description: "Message ID for pin/unpin operations" })
    @ValidateIf(o =>
        o.operation_type === GroupOperations.PIN_MESSAGES ||
        o.operation_type === GroupOperations.UNPIN_MESSAGES
    )
    @IsMongoId()
    @IsNotEmpty()
    message_id?: string;
}