import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Headers,
} from '@nestjs/common';
import { UserContactService } from '../../application/services/user-contact.service';
import {
  AddContactDto,
  UpdateContactValueDto,
  VerifyContactDto,
  EnableNotificationsDto,
  UpdateNotificationTypesDto,
  UserContactResponseDto,
  VerificationResponseDto,
} from '../../application/dtos/user-contact.dto';
import { UserContactEntity, ContactType } from '../../domain/entities/user-contact.entity';

@Controller('users/me/contacts')
export class UserContactController {
  constructor(private readonly contactService: UserContactService) {}

  @Get()
  async getContacts(@Headers('x-user-id') userId: string) {
    const contacts = await this.contactService.getContacts(userId);
    return {
      success: true,
      data: contacts.map(c => this.toResponseDto(c)),
    };
  }

  @Get('verified')
  async getVerifiedContacts(@Headers('x-user-id') userId: string) {
    const contacts = await this.contactService.getVerifiedContacts(userId);
    return {
      success: true,
      data: contacts.map(c => this.toResponseDto(c)),
    };
  }

  @Post()
  async addContact(
    @Headers('x-user-id') userId: string,
    @Body() dto: AddContactDto,
  ) {
    const contact = await this.contactService.addContact(userId, dto.type, dto.value);
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Get(':type')
  async getContact(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
  ) {
    const contact = await this.contactService.getContact(userId, type);
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Put(':type')
  async updateContactValue(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
    @Body() dto: UpdateContactValueDto,
  ) {
    const contact = await this.contactService.updateContactValue(userId, type, dto.value);
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Delete(':type')
  async deleteContact(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
  ) {
    await this.contactService.deleteContact(userId, type);
    return {
      success: true,
      message: 'Contact deleted',
    };
  }

  @Post(':type/verification')
  async requestVerification(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
  ): Promise<{ success: boolean; data: VerificationResponseDto }> {
    const result = await this.contactService.requestVerification(userId, type);
    return {
      success: true,
      data: {
        message: 'Verification code sent',
        expiresAt: result.expiresAt,
        // Include code in development mode only
        code: process.env.NODE_ENV === 'development' ? result.code : undefined,
      },
    };
  }

  @Put(':type/verification')
  async verifyContact(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
    @Body() dto: VerifyContactDto,
  ) {
    const contact = await this.contactService.verifyContact(userId, type, dto.code);
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Post(':type/notifications')
  async enableNotifications(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
    @Body() dto: EnableNotificationsDto,
  ) {
    const contact = await this.contactService.enableNotifications(
      userId,
      type,
      dto.notificationTypes,
    );
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Delete(':type/notifications')
  async disableNotifications(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
  ) {
    const contact = await this.contactService.disableNotifications(userId, type);
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  @Put(':type/notifications')
  async updateNotificationTypes(
    @Headers('x-user-id') userId: string,
    @Param('type') type: ContactType,
    @Body() dto: UpdateNotificationTypesDto,
  ) {
    const contact = await this.contactService.updateNotificationTypes(
      userId,
      type,
      dto.notificationTypes,
    );
    return {
      success: true,
      data: this.toResponseDto(contact),
    };
  }

  private toResponseDto(contact: UserContactEntity): UserContactResponseDto {
    return {
      id: contact.id,
      userId: contact.userId,
      type: contact.type,
      value: contact.value,
      displayName: contact.displayName,
      isVerified: contact.isVerified,
      verifiedAt: contact.verifiedAt,
      notificationEnabled: contact.notificationEnabled,
      enabledNotificationTypes: contact.enabledNotificationTypes,
      createdAt: contact.createdAt,
      updatedAt: contact.updatedAt,
    };
  }
}
