import { Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { CouponType } from '@prisma/client';
import { Roles } from '../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { CouponsService } from './coupons.service';

@Controller()
@UseGuards(JwtAuthGuard, RolesGuard)
export class CouponsController {
  constructor(private readonly couponsService: CouponsService) {}

  @Get('coupons')
  @Roles('RESTAURANT')
  listCoupons(@Req() req: { user: { sub: string; role: string } }) {
    return this.couponsService.listCoupons(req.user as never);
  }

  @Post('coupons')
  @Roles('RESTAURANT')
  createCoupon(
    @Req() req: { user: { sub: string; role: string } },
    @Body()
    body: {
      code: string;
      type: CouponType;
      value: number;
      minOrderValue?: number | null;
      usageLimit?: number | null;
      perUserLimit?: number | null;
      validFrom: string;
      validUntil: string;
      isActive?: boolean;
    },
  ) {
    return this.couponsService.createCoupon(req.user as never, body);
  }

  @Patch('coupons/:id')
  @Roles('RESTAURANT')
  updateCoupon(
    @Req() req: { user: { sub: string; role: string } },
    @Param('id') id: string,
    @Body()
    body: {
      code?: string;
      type?: CouponType;
      value?: number;
      minOrderValue?: number | null;
      usageLimit?: number | null;
      perUserLimit?: number | null;
      validFrom?: string;
      validUntil?: string;
      isActive?: boolean;
    },
  ) {
    return this.couponsService.updateCoupon(req.user as never, id, body);
  }

  @Delete('coupons/:id')
  @Roles('RESTAURANT')
  archiveCoupon(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.couponsService.archiveCoupon(req.user as never, id);
  }

  @Get('promotions')
  @Roles('RESTAURANT')
  listPromotions(@Req() req: { user: { sub: string; role: string } }) {
    return this.couponsService.listPromotions(req.user as never);
  }

  @Post('promotions')
  @Roles('RESTAURANT')
  createPromotion(
    @Req() req: { user: { sub: string; role: string } },
    @Body()
    body: {
      title: string;
      description?: string | null;
      discountType: CouponType;
      discountValue: number;
      startsAt: string;
      endsAt: string;
      isActive?: boolean;
    },
  ) {
    return this.couponsService.createPromotion(req.user as never, body);
  }

  @Patch('promotions/:id')
  @Roles('RESTAURANT')
  updatePromotion(
    @Req() req: { user: { sub: string; role: string } },
    @Param('id') id: string,
    @Body()
    body: {
      title?: string;
      description?: string | null;
      discountType?: CouponType;
      discountValue?: number;
      startsAt?: string;
      endsAt?: string;
      isActive?: boolean;
    },
  ) {
    return this.couponsService.updatePromotion(req.user as never, id, body);
  }

  @Delete('promotions/:id')
  @Roles('RESTAURANT')
  archivePromotion(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.couponsService.archivePromotion(req.user as never, id);
  }
}
