import {
  IsEmail,
  IsPhoneNumber,
  IsNumber,
  IsString,
  IsNotEmpty,
  MinLength,
  MaxLength,
  Matches,
  IsOptional,
} from 'class-validator';

/**
 * ═════════════════════════════════════════════════════════
 * DTO 1: INVITE CLIENT
 * ═════════════════════════════════════════════════════════
 * 
 * When coordinator clicks [Invite Client] button
 * Input: company_id, client_email, client_phone
 * Output: {token, link, message}
 */
export class InviteClientDto {
  /**
   * Company to invite client for
   */
  @IsNumber()
  @IsNotEmpty()
  company_id: number;

  /**
   * Client's email address
   */
  @IsEmail()
  @IsNotEmpty()
  client_email: string;

  /**
   * Client's phone (E.164 format: +971501234567)
   * Regex validates: + followed by 7-15 digits
   */
  @IsOptional()
  @Matches(/^\+[1-9]\d{1,14}$/, {
    message: 'Phone must be in E.164 format (e.g., +971501234567)',
  })
  client_phone?: string;
}

/**
 * ═════════════════════════════════════════════════════════
 * DTO 2: CLIENT LOGIN
 * ═════════════════════════════════════════════════════════
 * 
 * Client enters email on /client/login/:token
 * Input: email, token
 * Output: {otp_sent: true, message: "OTP sent to SMS"}
 */
export class ClientLoginDto {
  /**
   * Email — optional when using invite token (auto-resolved from token)
   */
  @IsEmail({}, { message: 'Please enter a valid email address' })
  @IsOptional()
  email?: string;

  /**
   * Token from invite link (URL param) — optional when using email-only login
   */
  @IsString()
  @IsOptional()
  token?: string;
}

/**
 * ═════════════════════════════════════════════════════════
 * DTO 3: VERIFY OTP
 * ═════════════════════════════════════════════════════════
 * 
 * Client enters 5-digit OTP
 * Input: email, otp_code
 * Output: {access_token, user, company_ids}
 */
export class VerifyOtpDto {
  /**
   * Client's email
   */
  @IsEmail()
  @IsNotEmpty()
  email: string;

  /**
   * 5-digit OTP code
   */
  @Matches(/^\d{5}$/, {
    message: 'OTP must be exactly 5 digits',
  })
  @IsNotEmpty()
  otp_code: string;

  @IsOptional()
  @IsString()
  device_name?: string;   // e.g. "iPhone 15 Pro", "Samsung Galaxy S24"

  @IsOptional()
  @IsString()
  device_type?: string;   // "ios" | "android" | "web"
}

/**
 * ═════════════════════════════════════════════════════════
 * DTO 4: LIST CLIENT AUDITS (Query)
 * ═════════════════════════════════════════════════════════
 * 
 * GET /client-portal/audits?status=SUBMITTED&limit=10&page=1
 */
export class ListClientAuditsDto {
  /**
   * Filter by status
   * SUBMITTED, SCHEDULED, IN_PROGRESS, COMPLETED, CERTIFIED
   */
  @IsOptional()
  @IsString()
  status?: string;

  /**
   * How many items per page (default 10, max 50)
   */
  @IsOptional()
  @IsNumber()
  limit?: number = 10;

  /**
   * Which page (default 1)
   */
  @IsOptional()
  @IsNumber()
  page?: number = 1;

  /**
   * Sort by field
   */
  @IsOptional()
  @IsString()
  sort_by?: string = 'created_at';

  /**
   * Sort direction
   */
  @IsOptional()
  @IsString()
  sort_order?: 'ASC' | 'DESC' = 'DESC';
}

/**
 * ═════════════════════════════════════════════════════════
 * DTO 5: RESEND OTP
 * ═════════════════════════════════════════════════════════
 * 
 * If client didn't receive OTP
 * POST /client-portal/resend-otp
 */
export class ResendOtpDto {
  /**
   * Client's email
   */
  @IsEmail()
  @IsNotEmpty()
  email: string;

  /**
   * Token from invite link
   */
  @IsString()
  @IsNotEmpty()
  token: string;
}
export class RefreshClientTokenDto {
  @IsString()
  @IsNotEmpty()
  refresh_token: string;
}
export class ClientLogoutDto {
  @IsString()
  @IsNotEmpty()
  refresh_token: string;
}