import { ArrayNotEmpty, IsArray, IsDateString, IsIn, IsInt, IsISO8601, IsString, IsNumber, IsOptional, Matches, Min, ValidateNested } from 'class-validator';
import { PartialType } from '@nestjs/mapped-types';
import { Type } from 'class-transformer';

export class CreateRoomExpenseDto {
  @IsInt()
  roomId: number;

  @IsString()
  expenseType: string;

  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  amount: number;

  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsISO8601({ strict: true })
  @IsDateString()
  periodStart: string;

  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsISO8601({ strict: true })
  @IsDateString()
  periodEnd: string;

  @IsOptional()
  @IsString()
  description?: string;
}

export class CreatePersonalExpenseDto {
  @IsInt()
  studentId: number;

  @IsOptional()
  @IsInt()
  roomId?: number;

  @IsString()
  expenseType: string;

  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  amount: number;

  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsISO8601({ strict: true })
  @IsDateString()
  expenseDate: string;

  @IsOptional()
  @IsString()
  description?: string;
}


export class UpdateRoomExpenseDto extends PartialType(CreateRoomExpenseDto) {}

export class UpdatePersonalExpenseDto extends PartialType(CreatePersonalExpenseDto) {}

export class QueryRoomExpenseDto {
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  roomId?: number;

  @IsOptional()
  @IsDateString()
  periodStart?: string;

  @IsOptional()
  @IsDateString()
  periodEnd?: string;
}

export class QueryPersonalExpenseDto {
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  studentId?: number;
}

export class BatchRoomExpenseItemDto {
  @IsInt()
  roomId: number;

  @IsString()
  expenseType: string;

  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  amount: number;

  @IsOptional()
  @IsString()
  description?: string;
}

export class BatchRoomExpenseDto {
  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsISO8601({ strict: true })
  @IsDateString()
  periodStart: string;

  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsISO8601({ strict: true })
  @IsDateString()
  periodEnd: string;

  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => BatchRoomExpenseItemDto)
  expenses: BatchRoomExpenseItemDto[];
}


export class CreateStudentUtilityBillDto {
  @IsInt()
  studentId: number;

  @IsIn(['water', 'electricity'])
  expenseType: 'water' | 'electricity';

  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  amount: number;

  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  periodStart: string;

  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  periodEnd: string;

  @IsOptional()
  @IsString()
  description?: string;
}
