import { Controller, Get, Post, Body, Param, Headers, Req } from '@nestjs/common';
import { Request } from 'express';
import { GiftService } from '../services/gift.service';
import { CreateGiftDto } from './dto/create-gift.dto';
import { CreateContributionDto } from './dto/create-contribution.dto';

@Controller('gift')
export class GiftController {
  constructor(private readonly giftService: GiftService) {}

  @Post()
  async createGift(
    @Req() req: Request,
    @Headers('x-tenant-id') tenantId: string,
    @Body() dto: CreateGiftDto,
  ) {
    const ownerId = (req as any).user?.id;
    return this.giftService.createGift(tenantId, { ...dto, ownerId });
  }

  @Get()
  async listGifts(@Headers('x-tenant-id') tenantId: string) {
    return this.giftService.listGifts(tenantId);
  }

  @Post('contribution')
  async contribute(
    @Headers('x-tenant-id') tenantId: string,
    @Body() dto: CreateContributionDto,
  ) {
    return this.giftService.createContribution(tenantId, dto);
  }

  @Get(':id')
  async getGift(
    @Req() req: Request,
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
  ) {
    const requesterId = (req as any).user?.id;
    return this.giftService.getGiftById(tenantId, id, requesterId);
  }

  @Get(':id/contributions')
  async listContributions(
    @Req() req: Request,
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
  ) {
    const requesterId = (req as any).user?.id;
    return this.giftService.listContributions(tenantId, id, requesterId);
  }
}
