import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
  ArrayMaxSize,
  IsArray,
  IsEnum,
  IsMongoId,
  IsNumber,
  IsOptional,
  IsBoolean,
  IsString,
  IsUrl,
  Length,
  Max,
  Min,
} from 'class-validator';
import { PostVisibility } from '../../../common/enums/post-visibility.enum';
import { toNumberArray, toStringArray } from '../../../common/utils/array-transform.util';

export class CreatePostDto {
  @ApiPropertyOptional({ maxLength: 2200, description: 'Post caption/content (optional with media)' })
  @IsOptional()
  @IsString()
  @Length(0, 2200)
  content?: string;

  @ApiPropertyOptional({ maxLength: 2200, description: 'Text shown above media' })
  @IsOptional()
  @IsString()
  @Length(0, 2200)
  contentTop?: string;

  @ApiPropertyOptional({ maxLength: 2200, description: 'Text shown below media' })
  @IsOptional()
  @IsString()
  @Length(0, 2200)
  contentBottom?: string;

  @ApiPropertyOptional({ description: 'Single video URL (optional)' })
  @IsOptional()
  @IsUrl({ require_tld: false })
  videoUrl?: string;

  @ApiPropertyOptional({ description: 'Single audio URL (optional)' })
  @IsOptional()
  @IsUrl({ require_tld: false })
  audioUrl?: string;

  @ApiPropertyOptional({ description: 'Media duration in seconds for audio/video posts' })
  @IsOptional()
  @IsNumber()
  @Min(1)
  @Max(7200)
  durationSeconds?: number;

  @ApiPropertyOptional({ description: 'Cover/thumbnail URL for audio/video posts' })
  @IsOptional()
  @IsUrl({ require_tld: false })
  thumbnailUrl?: string;

  @ApiPropertyOptional({ description: 'Music style or genre for audio/video posts', maxLength: 80 })
  @IsOptional()
  @IsString()
  @Length(0, 80)
  style?: string;

  @ApiPropertyOptional({ description: 'Maqam name for audio/video posts', maxLength: 80 })
  @IsOptional()
  @IsString()
  @Length(0, 80)
  maqam?: string;

  @ApiPropertyOptional({ description: 'Rhythm signature like 6/8 for audio/video posts', maxLength: 40 })
  @IsOptional()
  @IsString()
  @Length(0, 40)
  rhythmSignature?: string;

  @ApiPropertyOptional({
    type: [Number],
    description: 'Optional waveform samples for audio posts only',
  })
  @IsOptional()
  @Transform(toNumberArray)
  @IsArray()
  @ArrayMaxSize(512)
  @IsNumber({}, { each: true })
  waveformPeaks?: number[];

  @ApiPropertyOptional({ type: [String], description: 'Multiple image URLs for image carousel (max 10)' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(10)
  @IsUrl({ require_tld: false }, { each: true })
  imageUrls?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Caption per carousel image, matched by order' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(10)
  @IsString({ each: true })
  @Length(0, 300, { each: true })
  imageCaptions?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Alt text per carousel image, matched by order' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(10)
  @IsString({ each: true })
  @Length(0, 300, { each: true })
  imageAltTexts?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Tagged user ids (max 20)' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(20)
  @IsMongoId({ each: true })
  taggedUserIds?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Collaborator user ids (max 5)' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(5)
  @IsMongoId({ each: true })
  collaboratorIds?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Mention usernames like rami_sabry (max 30)' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(30)
  @IsString({ each: true })
  @Length(1, 30, { each: true })
  mentionUsernames?: string[];

  @ApiPropertyOptional({ type: [String], description: 'Mentioned user ids (max 30)' })
  @IsOptional()
  @Transform(toStringArray)
  @IsArray()
  @ArrayMaxSize(30)
  @IsMongoId({ each: true })
  mentionedUserIds?: string[];

  @ApiPropertyOptional({ description: 'Post location text' })
  @IsOptional()
  @IsString()
  @Length(0, 120)
  location?: string;

  @ApiPropertyOptional({ description: 'Post latitude' })
  @IsOptional()
  @IsNumber()
  @Min(-90)
  @Max(90)
  latitude?: number;

  @ApiPropertyOptional({ description: 'Post longitude' })
  @IsOptional()
  @IsNumber()
  @Min(-180)
  @Max(180)
  longitude?: number;

  @ApiPropertyOptional({ enum: PostVisibility, default: PostVisibility.PUBLIC })
  @IsOptional()
  @IsEnum(PostVisibility)
  visibility?: PostVisibility;

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  commentsDisabled?: boolean;

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  commentsFollowersOnly?: boolean;
}
