import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsOptional,
  IsArray,
  ValidateNested,
  IsNumberString,
  IsNumber,
} from 'class-validator';
import { Type } from 'class-transformer';

/**
 * DTO representing a single commission item related to a student or visa case.
 * Each item contains detailed financial and student information for tracking commission payments.
 */
export class ItemDto {
  /** Response or status for this item (e.g., approved, pending) */
  @ApiProperty({ description: 'Response status', example: 'approved' })
  @IsString()
  response: string;

  /** Date of commission in dd/mm/yyyy format */
  @ApiProperty({
    description: 'Commission date in dd/mm/yyyy format',
    example: '09/01/2026',
  })
  @IsString()
  commission_date: string;

  /** Difference between expected and actual commission or fees */
  @ApiProperty({ description: 'Difference amount', example: '50' })
  @IsString()
  difference: string;

  /** Reason for any difference in commission or fees */
  @ApiProperty({
    description: 'Reason for difference',
    example: 'Late submission',
  })
  @IsString()
  difference_reason: string;

  /** General remarks related to this commission item */
  @ApiProperty({ description: 'Remarks', example: 'Verified by finance team' })
  @IsString()
  remarks: string;

  /** Optional reason for the response (e.g., why approved/rejected) */
  @ApiPropertyOptional({
    description: 'Optional response reason',
    example: 'Verified documents',
  })
  @IsString()
  @IsOptional()
  response_reason?: string;

  /** Tuition fees for this student, as a string or number */
  @ApiProperty({ description: 'Tuition fees', example: '10000' })
  @IsNumberString()
  tuition_fees: string | number;

  /** Type of commission (e.g., fixed, percentage) */
  @ApiProperty({ description: 'Commission type', example: 'percentage' })
  @IsString()
  commission_type: string;

  /** Optional commission percentage (required if type is percentage) */
  @ApiPropertyOptional({ description: 'Commission percentage', example: '10' })
  @IsNumberString()
  @IsOptional()
  commission_percentage?: string | number;

  /** Calculated commission amount for this item */
  @ApiProperty({ description: 'Commission amount', example: '1000' })
  @IsNumberString()
  commission_amount: string | number;

  /** Currency of tuition fees */
  @ApiProperty({ description: 'Tuition fees currency', example: 'USD' })
  @IsString()
  tuition_fees_currency: string;

  /** Currency of commission amount */
  @ApiProperty({ description: 'Commission currency', example: 'USD' })
  @IsString()
  commission_currency: string;

  /** Unique identifier for the case */
  @ApiProperty({ description: 'Case UID', example: 'CASE01115' })
  @IsString()
  case_uid: string;

  /** Student's first name */
  @ApiProperty({ description: 'First name', example: 'John' })
  @IsString()
  fname: string;

  /** Student's last name */
  @ApiProperty({ description: 'Last name', example: 'Doe' })
  @IsString()
  lname: string;

  /** Unique student ID */
  @ApiProperty({ description: 'Student UID', example: 'STU099695' })
  @IsString()
  student_uid: string;

  /** Optional internal record ID */
  @ApiPropertyOptional({ description: 'Internal record ID', example: 123 })
  @IsOptional()
  @IsNumber()
  id?: number;

  /** Duplicate of case UID for consistency */
  @ApiProperty({ description: 'Visa case ID', example: 'CASE01115' })
  @IsString()
  visa_case_id: string;

  /** Optional group index for batch processing */
  @ApiPropertyOptional({ description: 'Group index for batch', example: 1 })
  @IsOptional()
  group_index?: any;

  /** Optional group ID for batch processing */
  @ApiPropertyOptional({ description: 'Group ID', example: 'G123' })
  @IsOptional()
  group_id?: any;

  /** Optional total of group, used for validation */
  @ApiPropertyOptional({ description: 'Total in group', example: 5 })
  @IsOptional()
  group_total?: any;

  /** Optional field tracking who modified this record */
  @ApiPropertyOptional({
    description: 'Modified by user',
    example: 'admin_user',
  })
  @IsOptional()
  modified_by_user?: any;

  /** Optional ID for response reason */
  @ApiPropertyOptional({ description: 'Response reason ID', example: 1 })
  @IsOptional()
  response_reason_id?: any;

  /** Optional ID for difference reason */
  @ApiPropertyOptional({ description: 'Difference reason ID', example: 1 })
  @IsOptional()
  difference_reason_id?: any;

  /** Optional flag indicating if the commission was revised */
  @ApiPropertyOptional({ description: 'Is revised?', example: true })
  @IsOptional()
  is_revised?: any;
}

/**
 * DTO used to create a commission response for multiple students/cases.
 * This includes financial, student, and invoice information.
 */
export class CreateResponseDto {
  /** Optional association type for MA (e.g., agent, institute) */
  @ApiPropertyOptional({ description: 'Association type', example: '1' })
  @IsOptional()
  @IsString()
  association_type?: string;

  /** ID of visa service country */
  @ApiProperty({ description: 'Visa service country ID', example: '101' })
  @IsString()
  visa_service_country_id: string;

  /** ID of educational institute */
  @ApiProperty({ description: 'Institute ID', example: '459' })
  @IsString()
  institute_id: string;

  /** ID of client’s originating country */
  @ApiProperty({ description: 'Client from country ID', example: '80' })
  @IsString()
  client_from_country_id: string;

  /** Intake/session ID */
  @ApiProperty({ description: 'Intake ID', example: '95504' })
  @IsString()
  intake_id: string;

  /** Indicates what this commission applies to */
  @ApiProperty({ description: 'Applicable on ID', example: '2' })
  @IsString()
  applicable_on_id: string;

  /** Contract set ID */
  @ApiProperty({ description: 'Contract set ID', example: '2' })
  @IsString()
  contract_set_id: string;

  /** Commission instance ID */
  @ApiProperty({ description: 'Commission instance ID', example: '3' })
  @IsString()
  commission_instance_id: string;

  /** Branch ID for invoicing */
  @ApiProperty({ description: 'Invoice branch ID', example: '3' })
  @IsString()
  invoice_branch_id: string;

  /** Bank account ID for invoicing */
  @ApiProperty({ description: 'Invoice bank account ID', example: '4' })
  @IsString()
  invoice_bank_account_id: string;

  /** Array of commission items, each representing a student/case */
  @ApiProperty({ type: [ItemDto], description: 'List of commission items' })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ItemDto)
  items: ItemDto[];

  /** Unique record ID */
  @ApiProperty({ description: 'Record ID', example: '123' })
  @IsNumberString()
  id: string | number;

  /** Currency code for this response */
  @ApiProperty({ description: 'Currency code', example: 'USD' })
  @IsString()
  currency: string;

  /** Total net amount calculated for all items */
  @ApiProperty({ description: 'Net amount', example: '408.18' })
  @IsNumberString()
  net_amount: string | number;

  /** Optional accept status (e.g., 1 = approved, 0 = pending) */
  @ApiProperty({ description: 'Accept status', example: '1' })
  @IsNumberString()
  accept_status?: string | number;
}
