import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { IsBoolean, IsOptional, IsString, MaxLength, IsInt, IsUrl, IsNotEmpty } from 'class-validator';
import { PesCampaignResponseDto } from './pes-campaign.dto';
import { PesLandingPage } from '../entities/pes-laning-pages.entity';

export class CreatePesLandingPageDto {
  @ApiProperty({
    example: 'Phishing Login Page',
    description: 'Name of the landing page',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  @IsNotEmpty()
  name: string;

  @ApiPropertyOptional({
    example: 'This landing page simulates a login screen.',
    description: 'Optional description of the landing page',
  })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiPropertyOptional({
    example: '<html><body><form>...</form></body></html>',
    description: 'Optional HTML content of the landing page',
  })
  @IsString()
  @IsNotEmpty()
  htmlContent: string;

  @ApiPropertyOptional({
    example: true,
    description: 'Whether the page captures credentials from form submissions',
  })
  @IsBoolean()
  @IsNotEmpty()
  captureCredentials: boolean;

  @ApiPropertyOptional({
    example: true,
    description: 'Whether the page captures passwords from form submissions',
  })
  @IsOptional()
  @IsBoolean()
  @IsNotEmpty()
  capturePasswords: boolean;

  @ApiPropertyOptional({
    example: 'https://example.com/thank-you',
    description: 'URL to redirect users after form submission',
  })
  @IsOptional()
  @IsUrl({}, { message: 'redirectUrl must be a valid URL' })
  redirectUrl?: string;

  @ApiProperty({
    example: 'user-123',
    description: 'ID of the user who created the landing page',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  @IsNotEmpty()
  verifiedUserId: string;

  @ApiPropertyOptional({
    example: true,
    description: 'Whether the landing page is currently active',
  })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @ApiPropertyOptional({
    example: 100,
    description: 'Number of times the page has been visited',
  })
  @IsOptional()
  @IsInt()
  visitCount?: number;

  @ApiPropertyOptional({
    example: 45,
    description: 'Number of times users submitted the form on this page',
  })
  @IsOptional()
  @IsInt()
  submissionCount?: number;
}

export class UpdatePesLandingPageDto extends PartialType(CreatePesLandingPageDto) {}

export class PesLandingPageResponseDto {
  @ApiProperty()
  id: string;

  @ApiProperty()
  pageId: string;

  @ApiProperty()
  name: string;

  @ApiProperty({ required: false })
  description: string;

  @ApiProperty({ required: false })
  htmlContent: string;

  @ApiProperty()
  captureCredentials: boolean;

  @ApiProperty()
  capturePasswords: boolean;

  @ApiProperty({ required: false })
  redirectUrl: string;

  @ApiProperty()
  verifiedUserId: string;

  @ApiProperty()
  isActive: boolean;

  @ApiProperty()
  visitCount: number;

  @ApiProperty()
  submissionCount: number;

  @ApiProperty({ type: String, format: 'date-time' })
  createdAt: Date;

  @ApiProperty({ type: String, format: 'date-time' })
  updatedAt: Date;

  @ApiPropertyOptional({ type: () => [PesCampaignResponseDto] })
  campaigns?: PesCampaignResponseDto[] | null;

  constructor(landingPage: PesLandingPage) {
    this.id = landingPage.id;
    this.pageId = landingPage.pageId;
    this.name = landingPage.name;
    this.description = landingPage.description || '';
    this.htmlContent = landingPage.htmlContent || '';
    this.captureCredentials = landingPage.captureCredentials;
    this.capturePasswords = landingPage.capturePasswords;
    this.redirectUrl = landingPage.redirectUrl || '';
    this.verifiedUserId = landingPage.verifiedUserId;
    this.isActive = landingPage.isActive;
    this.visitCount = landingPage.visitCount || 0;
    this.submissionCount = landingPage.submissionCount || 0;
    this.createdAt = landingPage.createdAt;
    this.updatedAt = landingPage.updatedAt;
  }
}
