import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  HttpCode,
  HttpStatus,
  Query,
} from '@nestjs/common';
import { ChildrenService } from './children.service';
import { CreateChildDto } from './dto/create-child.dto';
import { UpdateChildDto } from './dto/update-child.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';

@Controller('api/v1/children')
@UseGuards(JwtAuthGuard)
export class ChildrenController {
  constructor(private readonly childrenService: ChildrenService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  async create(
    @CurrentUser() user: any,
    @Query('familyId') familyId: string,
    @Body() createChildDto: CreateChildDto,
  ) {
    if (!familyId) {
      return {
        success: false,
        error: {
          code: 'VALIDATION_ERROR',
          message: 'familyId query parameter is required',
        },
      };
    }

    const child = await this.childrenService.create(
      user.sub,
      familyId,
      createChildDto,
    );

    return {
      success: true,
      data: {
        child: {
          id: child.id,
          familyId: child.familyId,
          name: child.name,
          birthDate: child.birthDate,
          gender: child.gender,
          photoUrl: child.photoUrl,
          medicalInfo: child.medicalInfo,
          createdAt: child.createdAt,
        },
      },
    };
  }

  @Get()
  @HttpCode(HttpStatus.OK)
  async findAll(@CurrentUser() user: any, @Query('familyId') familyId: string) {
    let children;

    if (familyId) {
      // Get children for specific family
      children = await this.childrenService.findAll(user.sub, familyId);
    } else {
      // Get all children across all families
      children = await this.childrenService.findAllForUser(user.sub);
    }

    return {
      success: true,
      data: {
        children: children.map((child) => ({
          id: child.id,
          familyId: child.familyId,
          name: child.name,
          birthDate: child.birthDate,
          gender: child.gender,
          photoUrl: child.photoUrl,
          medicalInfo: child.medicalInfo,
          createdAt: child.createdAt,
        })),
      },
    };
  }

  @Get(':id')
  @HttpCode(HttpStatus.OK)
  async findOne(@CurrentUser() user: any, @Param('id') id: string) {
    const child = await this.childrenService.findOne(user.sub, id);

    return {
      success: true,
      data: {
        child: {
          id: child.id,
          familyId: child.familyId,
          name: child.name,
          birthDate: child.birthDate,
          gender: child.gender,
          photoUrl: child.photoUrl,
          medicalInfo: child.medicalInfo,
          createdAt: child.createdAt,
        },
      },
    };
  }

  @Get(':id/age')
  @HttpCode(HttpStatus.OK)
  async getChildAge(@Param('id') id: string) {
    const ageInMonths = await this.childrenService.getChildAgeInMonths(id);

    return {
      success: true,
      data: {
        ageInMonths,
        ageInYears: Math.floor(ageInMonths / 12),
        remainingMonths: ageInMonths % 12,
      },
    };
  }

  @Patch(':id')
  @HttpCode(HttpStatus.OK)
  async update(
    @CurrentUser() user: any,
    @Param('id') id: string,
    @Body() updateChildDto: UpdateChildDto,
  ) {
    const child = await this.childrenService.update(
      user.sub,
      id,
      updateChildDto,
    );

    return {
      success: true,
      data: {
        child: {
          id: child.id,
          familyId: child.familyId,
          name: child.name,
          birthDate: child.birthDate,
          gender: child.gender,
          photoUrl: child.photoUrl,
          medicalInfo: child.medicalInfo,
          createdAt: child.createdAt,
        },
      },
    };
  }

  @Delete(':id')
  @HttpCode(HttpStatus.OK)
  async remove(@CurrentUser() user: any, @Param('id') id: string) {
    await this.childrenService.remove(user.sub, id);

    return {
      success: true,
      message: 'Child deleted successfully',
    };
  }
}
