import { Controller, Get, Post, Put, Delete, Param, Query, Body } from '@nestjs/common';
import { SegmentsService, SegmentMetrics, SegmentComparison } from './segments.service';
import { Segment } from './segment.entity';
import {
  CreateSegmentDto,
  UpdateSegmentDto,
  ApplySegmentQueryDto,
  CompareSegmentsQueryDto,
} from './dto/segment.dto';

@Controller('segments')
export class SegmentsController {
  constructor(private readonly segmentsService: SegmentsService) {}

  /**
   * Get all segments
   * GET /segments
   */
  @Get()
  async findAll(): Promise<Segment[]> {
    return this.segmentsService.findAll();
  }

  /**
   * Get segment by ID
   * GET /segments/:id
   */
  @Get(':id')
  async findById(@Param('id') id: string): Promise<Segment> {
    return this.segmentsService.findById(id);
  }

  /**
   * Create new segment
   * POST /segments
   */
  @Post()
  async create(@Body() dto: CreateSegmentDto): Promise<Segment> {
    return this.segmentsService.create(dto);
  }

  /**
   * Update segment
   * PUT /segments/:id
   */
  @Put(':id')
  async update(@Param('id') id: string, @Body() dto: UpdateSegmentDto): Promise<Segment> {
    return this.segmentsService.update(id, dto);
  }

  /**
   * Delete segment
   * DELETE /segments/:id
   */
  @Delete(':id')
  async delete(@Param('id') id: string): Promise<void> {
    return this.segmentsService.delete(id);
  }

  /**
   * Apply segment to get filtered metrics
   * GET /segments/:id/apply?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get(':id/apply')
  async applySegment(
    @Param('id') id: string,
    @Query() query: ApplySegmentQueryDto,
  ): Promise<SegmentMetrics> {
    return this.segmentsService.applySegment(id, query);
  }

  /**
   * Compare multiple segments
   * GET /segments/compare?segmentIds=id1,id2&startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('compare')
  async compare(@Query() query: CompareSegmentsQueryDto): Promise<SegmentComparison> {
    return this.segmentsService.compareSegments(query);
  }
}
