import { Transform } from 'class-transformer'
import { IsNumberString, IsOptional, IsString } from 'class-validator'

export class BootstrapTableRequestDto {
    @IsOptional()
    @IsNumberString()
    limit?: string

    @IsOptional()
    @IsNumberString()
    offset?: string

    @IsOptional()
    @IsString()
    search?: string

    @IsOptional()
    @IsString()
    sort?: string

    @IsOptional()
    @IsString()
    order?: 'asc' | 'desc'

    @IsOptional()
    @Transform(({ value }) => {
        if (typeof value === 'object') return value
        try { return JSON.parse(value) } catch { return {} }
    })
    filter?: Record<string, any>  // ✅ remove @IsObject() — it conflicts with whitelist

    get take(): number {
        return this.limit ? parseInt(this.limit, 10) : 10
    }

    get skip(): number {
        return this.offset ? parseInt(this.offset, 10) : 0
    }

    get sortOrder(): 'asc' | 'desc' {
        return this.order === 'desc' ? 'desc' : 'asc'
    }
}