import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty, IsEnum, ValidateNested, IsDateString, IsOptional, IsArray, IsBoolean, IsUrl, IsNumber } from 'class-validator';
import { Type } from 'class-transformer';
import { JobLocation, JobContentSection } from '../entity/job.entity';

export class CreateJobLocationDto {
  @ApiProperty({
    enum: ['Remote', 'Hybrid', 'On-site'],
    description: 'Type of job location',
  })
  @IsEnum(['Remote', 'Hybrid', 'On-site'] as const)
  @IsNotEmpty()
  type: JobLocation;

  @ApiProperty({
    example: 'Americas, Europe',
    description: 'Geographic region for the job',
  })
  @IsString()
  @IsNotEmpty()
  region: string;
}

export class CreateJobContentSectionDto implements JobContentSection {
  @ApiProperty({
    example: 'Responsibilities',
    description: 'Title of the content section',
  })
  @IsString()
  @IsNotEmpty()
  title: string;

  @ApiProperty({
    example: ['Build frontend applications', 'Collaborate with designers'],
    description: 'Array of content items for this section',
  })
  @IsArray()
  @IsString({ each: true })
  content: string[];

  @ApiProperty({
    example: 1,
    description: 'Order position of this section',
    required: false,
  })
  @IsNumber()
  @IsOptional()
  order?: number;
}

export class CreateJobSalaryRangeDto {
  @ApiProperty({
    example: 80000,
    description: 'Minimum salary',
    required: false,
  })
  @IsNumber()
  @IsOptional()
  min?: number;

  @ApiProperty({
    example: 120000,
    description: 'Maximum salary',
    required: false,
  })
  @IsNumber()
  @IsOptional()
  max?: number;

  @ApiProperty({
    example: 'USD',
    description: 'Currency for salary',
    required: false,
  })
  @IsString()
  @IsOptional()
  currency?: string;
}

export class CreateJobDto {
  @ApiProperty({
    example: 'Senior Frontend Engineer',
    description: 'Job title',
  })
  @IsString()
  @IsNotEmpty()
  title: string;

  @ApiProperty({
    example: 'Engineering',
    description: 'Department for the job',
  })
  @IsString()
  @IsNotEmpty()
  department: string;

  @ApiProperty({
    description: 'Location information for the job',
    type: CreateJobLocationDto,
  })
  @ValidateNested()
  @Type(() => CreateJobLocationDto)
  location: CreateJobLocationDto;

  @ApiProperty({
    example: 'We\'re looking for a Senior Frontend Engineer...',
    description: 'Detailed job description',
  })
  @IsString()
  @IsNotEmpty()
  description: string;

  @ApiProperty({
    description: 'Structured content sections for the job',
    type: [CreateJobContentSectionDto],
    required: false,
  })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateJobContentSectionDto)
  @IsOptional()
  contentSections?: CreateJobContentSectionDto[];

  @ApiProperty({
    description: 'Salary range information',
    type: CreateJobSalaryRangeDto,
    required: false,
  })
  @ValidateNested()
  @Type(() => CreateJobSalaryRangeDto)
  @IsOptional()
  salaryRange?: CreateJobSalaryRangeDto;

  @ApiProperty({
    example: '2025-04-01',
    description: 'Date when the job was posted',
    required: false,
  })
  @IsDateString()
  @IsOptional()
  postedAt?: string;

  @ApiProperty({
    example: true,
    description: 'Whether the job listing is active',
    required: false,
    default: true,
  })
  @IsBoolean()
  @IsOptional()
  isActive?: boolean;

  @ApiProperty({
    example: 'https://company.com/apply/senior-frontend',
    description: 'URL for direct application',
    required: false,
  })
  @IsUrl()
  @IsOptional()
  applicationUrl?: string;
}