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

/**
 * DTO representing individual commission item details for a student/visa case.
 */
export class ItemDto {
  /** Response status for the item (e.g., "approved", "pending") */
  @ApiProperty({ description: 'Response status for the item' })
  @IsString()
  response: string;

  /** Commission date in dd/mm/yyyy format */
  @ApiProperty({ description: 'Date in dd/mm/yyyy format' })
  @IsString()
  commission_date: string;

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

  /** Reason for the difference */
  @ApiProperty({ description: 'Reason for the difference' })
  @IsString()
  difference_reason: string;

  /** Additional remarks for the item */
  @ApiProperty({ description: 'Remarks for the item' })
  @IsString()
  remarks: string;

  /** Optional explanation for the response */
  @ApiPropertyOptional({ description: 'Optional explanation for the response' })
  @IsString()
  @IsOptional()
  response_reason?: string;

  /** Tuition fees for the student; string or number accepted */
  @ApiProperty({ description: 'Tuition fees', type: String })
  @IsNumberString()
  tuition_fees: string | number;

  /** Type of commission applied (e.g., "percentage", "flat") */
  @ApiProperty({ description: 'Type of commission' })
  @IsString()
  commission_type: string;

  /** Optional commission percentage (if applicable) */
  @ApiPropertyOptional({ description: 'Optional commission percentage' })
  @IsNumberString()
  @IsOptional()
  commission_percentage?: string | number;

  /** Commission amount */
  @ApiProperty({ description: 'Commission amount' })
  @IsNumberString()
  commission_amount: string | number;

  /** Currency code of tuition fees (e.g., "USD", "INR") */
  @ApiProperty({ description: 'Currency of tuition fees' })
  @IsString()
  tuition_fees_currency: string;

  /** Currency code of commission (e.g., "USD", "INR") */
  @ApiProperty({ description: 'Currency of commission' })
  @IsString()
  commission_currency: string;

  /** Unique case identifier */
  @ApiProperty({ description: 'Unique case identifier' })
  @IsString()
  case_uid: string;

  /** Student first name */
  @ApiProperty({ description: 'Student first name' })
  @IsString()
  fname: string;

  /** Student last name */
  @ApiProperty({ description: 'Student last name' })
  @IsString()
  lname: string;

  /** Unique student identifier */
  @ApiProperty({ description: 'Unique student identifier' })
  @IsString()
  student_uid: string;

  /** Optional internal ID of the item (database primary key) */
  @ApiPropertyOptional({ description: 'Internal item ID' })
  @IsOptional()
  @IsNumber()
  id?: number;

  /** Visa case ID associated with this item */
  @ApiProperty({ description: 'Visa case ID' })
  @IsString()
  visa_case_id: string;

  /** Optional grouping information for batch processing */
  @ApiPropertyOptional({ description: 'Group index for batch grouping' })
  @IsOptional()
  group_index?: any;

  @ApiPropertyOptional({ description: 'Group ID for batch grouping' })
  @IsOptional()
  group_id?: any;

  @ApiPropertyOptional({ description: 'Total number of items in the group' })
  @IsOptional()
  group_total?: any;

  @ApiPropertyOptional({ description: 'User who last modified this item' })
  @IsOptional()
  modified_by_user?: any;

  @ApiPropertyOptional({ description: 'Response reason ID reference' })
  @IsOptional()
  response_reason_id?: any;

  @ApiPropertyOptional({ description: 'Difference reason ID reference' })
  @IsOptional()
  difference_reason_id?: any;

  @ApiPropertyOptional({
    description: 'Flag indicating if the item was revised',
  })
  @IsOptional()
  is_revised?: any;

  /** Master agent commission type */
  @ApiPropertyOptional({ description: 'MA commission type' })
  @IsOptional()
  @IsString()
  ma_commission_type?: string;

  /** Master agent commission percentage */
  @ApiPropertyOptional({ description: 'MA commission percentage' })
  @IsNumberString()
  @IsOptional()
  ma_commission_percentage?: string | number;

  /** Master agent commission amount */
  @ApiPropertyOptional({ description: 'MA commission amount' })
  @IsNumberString()
  @IsOptional()
  ma_commission_amount?: string | number;
}

/**
 * DTO for creating Master Agent (MA) responses.
 * Contains invoice and commission details along with associated items.
 */
export class CreateMaResponseDto {
  /** Optional association type (e.g., "INSTITUTE") */
  @ApiPropertyOptional({ description: 'Optional association type' })
  @IsOptional()
  @IsString()
  association_type?: string;

  /** Optional associate ID */
  @ApiPropertyOptional({ description: 'Optional associate ID' })
  @IsOptional()
  @IsString()
  associate_id?: string;

  /** Visa service country ID */
  @ApiProperty({ description: 'Visa service country ID' })
  @IsString()
  visa_service_country_id: string;

  /** Institute ID */
  @ApiProperty({ description: 'Institute ID' })
  @IsString()
  institute_id: string;

  /** Client originating country ID */
  @ApiProperty({ description: 'Client from country ID' })
  @IsString()
  client_from_country_id: string;

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

  /** Applicable on ID (e.g., commission applicable on) */
  @ApiProperty({ description: 'Applicable on ID' })
  @IsString()
  applicable_on_id: string;

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

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

  /** Invoice branch ID */
  @ApiProperty({ description: 'Invoice branch ID' })
  @IsString()
  invoice_branch_id: string;

  /** Invoice bank account ID */
  @ApiProperty({ description: 'Invoice bank account ID' })
  @IsString()
  invoice_bank_account_id: string;

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

  /** Response ID */
  @ApiProperty({ description: 'Response ID' })
  @IsNumberString()
  id: string | number;

  /** Currency code for the invoice */
  @ApiProperty({ description: 'Currency code' })
  @IsString()
  currency: string;

  /** Net amount in the selected currency */
  @ApiProperty({ description: 'Net amount' })
  @IsNumberString()
  net_amount: string | number;

  /** Optional accept status (approved, rejected, pending) */
  @ApiProperty({ description: 'Optional accept status' })
  @IsNumberString()
  @IsOptional()
  accept_status?: string | number;

  /** Optional MA commission type */
  @ApiPropertyOptional({ description: 'Master Agent commission type' })
  @IsOptional()
  @IsString()
  ma_commission_type?: string;

  /** Optional MA commission percentage */
  @ApiPropertyOptional({ description: 'Master Agent commission percentage' })
  @IsOptional()
  ma_commission_percentage?: string | number;

  /** Optional MA commission amount */
  @ApiPropertyOptional({ description: 'Master Agent commission amount' })
  @IsOptional()
  @IsNumberString()
  ma_commission_amount?: string | number;
}
