import {
  Controller,
  Get,
  Put,
  Body,
  Headers,
  Query,
} from '@nestjs/common';
import { UserProfileService } from '../../application/services/user-profile.service';
import {
  UpdateBasicInfoDto,
  UpdateImmigrationIntentDto,
  UpdateEducationDto,
  UpdateWorkExperienceDto,
  UpdateLanguageScoresDto,
  UpdateFamilyMembersDto,
  UpdateFinancialInfoDto,
  UserProfileResponseDto,
} from '../../application/dtos/user-profile.dto';
import { UserProfileEntity } from '../../domain/entities/user-profile.entity';

@Controller('users/me/profile')
export class UserProfileController {
  constructor(private readonly profileService: UserProfileService) {}

  @Get()
  async getProfile(@Headers('x-user-id') userId: string) {
    const profile = await this.profileService.getOrCreateProfile(userId);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('basic-info')
  async updateBasicInfo(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateBasicInfoDto,
  ) {
    const data = {
      ...dto,
      birthDate: dto.birthDate ? new Date(dto.birthDate) : undefined,
    };
    const profile = await this.profileService.updateBasicInfo(userId, data);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('immigration-intent')
  async updateImmigrationIntent(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateImmigrationIntentDto,
  ) {
    const profile = await this.profileService.updateImmigrationIntent(userId, dto);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('education')
  async updateEducation(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateEducationDto,
  ) {
    const profile = await this.profileService.updateEducation(userId, dto);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('work-experience')
  async updateWorkExperience(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateWorkExperienceDto,
  ) {
    const profile = await this.profileService.updateWorkExperience(userId, dto);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('language-scores')
  async updateLanguageScores(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateLanguageScoresDto,
  ) {
    const profile = await this.profileService.updateLanguageScores(userId, dto.scores);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('family-members')
  async updateFamilyMembers(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateFamilyMembersDto,
  ) {
    const profile = await this.profileService.updateFamilyMembers(userId, dto.members);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  @Put('financial-info')
  async updateFinancialInfo(
    @Headers('x-user-id') userId: string,
    @Body() dto: UpdateFinancialInfoDto,
  ) {
    const profile = await this.profileService.updateFinancialInfo(userId, dto);
    return {
      success: true,
      data: this.toResponseDto(profile),
    };
  }

  private toResponseDto(profile: UserProfileEntity): UserProfileResponseDto {
    return {
      id: profile.id,
      userId: profile.userId,
      fullName: profile.fullName,
      birthDate: profile.birthDate?.toISOString().split('T')[0] ?? null,
      age: profile.getAge(),
      nationality: profile.nationality,
      currentCountry: profile.currentCountry,
      currentCity: profile.currentCity,
      maritalStatus: profile.maritalStatus,
      targetCountries: profile.targetCountries,
      immigrationTypes: profile.immigrationTypes,
      plannedTimeline: profile.plannedTimeline,
      primaryPurpose: profile.primaryPurpose,
      highestEducation: profile.highestEducation,
      educationRecords: profile.educationRecords,
      currentOccupation: profile.currentOccupation,
      totalWorkYears: profile.totalWorkYears,
      workRecords: profile.workRecords,
      languageScores: profile.languageScores,
      familyMembers: profile.familyMembers,
      netWorthRange: profile.netWorthRange,
      hasBusinessExperience: profile.hasBusinessExperience,
      businessYears: profile.businessYears,
      hasOverseasExperience: profile.hasOverseasExperience,
      completionPercentage: profile.completionPercentage,
      createdAt: profile.createdAt,
      updatedAt: profile.updatedAt,
    };
  }
}
