import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  UploadedFiles,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { FileFieldsInterceptor } from '@nestjs/platform-express';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Roles } from '../../common/decorators/roles.decorator';
import { SuperAdminPermissions } from '../../common/decorators/superadmin-permissions.decorator';
import { Throttle } from '../../common/decorators/throttle.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { RolesGuard } from '../../common/guards/roles.guard';
import { SuperAdminPermissionsGuard } from '../../common/guards/superadmin-permissions.guard';
import { SuperAdminJwtAuthGuard } from '../../common/guards/super-admin-jwt-auth.guard';
import { UserRole } from '../../common/enums/user-role.enum';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { MEDIA_MAX_SIZE_BYTES } from '../../common/media/allowed-media';
import { CreateInstrumentDto } from './dto/create-instrument.dto';
import { CreateRepairShopDto } from './dto/create-repair-shop.dto';
import { InstrumentQueryDto } from './dto/instrument-query.dto';
import { MarketplaceHomeQueryDto } from './dto/marketplace-home-query.dto';
import { RepairShopQueryDto } from './dto/repair-shop-query.dto';
import { UpdateShopProfileDto } from './dto/update-shop-profile.dto';
import { UpdateInstrumentDto } from './dto/update-instrument.dto';
import { UpdateMarketplaceStatusDto } from './dto/update-marketplace-status.dto';
import { UpdateRepairShopDto } from './dto/update-repair-shop.dto';
import { MarketplaceService } from './marketplace.service';
import { SUPERADMIN_PERMISSIONS } from '../superadmin/superadmin-permissions';

@ApiTags('Marketplace')
@Controller('marketplace')
export class MarketplaceController {
  constructor(private readonly marketplaceService: MarketplaceService) {}

  @Get('home')
  async getMarketplaceHome(@Query() query: MarketplaceHomeQueryDto) {
    return this.marketplaceService.getHome(query);
  }

  @Get('listings')
  async listPublicListings(@Query() query: InstrumentQueryDto) {
    return this.marketplaceService.getPublicListings(query);
  }

  @Get('listings/:id')
  async findListing(@Param('id') listingId: string) {
    return this.marketplaceService.findListingById(listingId);
  }

  @Get('instruments')
  async listPublic(@Query() query: InstrumentQueryDto) {
    return this.marketplaceService.getPublicInstruments(query);
  }

  @Get('instruments/:id')
  async findOne(@Param('id') instrumentId: string) {
    return this.marketplaceService.findInstrumentById(instrumentId);
  }

  @Get('repair-shops')
  async listPublicRepairShops(@Query() query: RepairShopQueryDto) {
    return this.marketplaceService.getPublicRepairShops(query);
  }

  @Get('repair-shops/:id')
  async findRepairShop(@Param('id') repairShopId: string) {
    return this.marketplaceService.findRepairShopById(repairShopId);
  }

  @Get('shops/:adminId')
  async getShopByAdminId(@Param('adminId') adminId: string) {
    return this.marketplaceService.getShopProfileByAdminId(adminId);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Get('superadmin/listings')
  async listAllListingsForSuperAdmin(@Query() query: InstrumentQueryDto) {
    return this.marketplaceService.getListingsForSuperAdmin(query);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Patch('superadmin/listings/:id/status')
  async updateListingStatusBySuperAdmin(
    @CurrentUser() user: JwtPayload,
    @Param('id') listingId: string,
    @Body() dto: UpdateMarketplaceStatusDto,
  ) {
    return this.marketplaceService.updateListingStatusBySuperAdmin(
      user.email ?? user.sub,
      listingId,
      dto,
    );
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Delete('superadmin/listings/:id')
  async deleteListingBySuperAdmin(@CurrentUser() user: JwtPayload, @Param('id') listingId: string) {
    await this.marketplaceService.removeListingBySuperAdmin(user.email ?? user.sub, listingId);
    return { success: true };
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Get('superadmin/repair-shops')
  async listAllRepairShopsForSuperAdmin(@Query() query: RepairShopQueryDto) {
    return this.marketplaceService.getRepairShopsForSuperAdmin(query);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Patch('superadmin/repair-shops/:id/status')
  async updateRepairShopStatusBySuperAdmin(
    @CurrentUser() user: JwtPayload,
    @Param('id') repairShopId: string,
    @Body() dto: UpdateMarketplaceStatusDto,
  ) {
    return this.marketplaceService.updateRepairShopStatusBySuperAdmin(
      user.email ?? user.sub,
      repairShopId,
      dto,
    );
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @Delete('superadmin/repair-shops/:id')
  async deleteRepairShopBySuperAdmin(@CurrentUser() user: JwtPayload, @Param('id') repairShopId: string) {
    await this.marketplaceService.removeRepairShopBySuperAdmin(user.email ?? user.sub, repairShopId);
    return { success: true };
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Professional Oud' },
        description: { type: 'string', example: 'Well-maintained oud for studio work' },
        price: { type: 'number', example: 3500 },
        currency: { type: 'string', example: 'SAR' },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        isActive: { type: 'boolean', example: true },
        condition: { type: 'string', example: 'used' },
        instrumentType: { type: 'string', example: 'Oud' },
        listingCategory: { type: 'string', example: 'musical_instrument' },
      },
      required: ['title', 'price', 'quantity'],
    },
  })
  @Post('superadmin/admins/:adminId/listings')
  @Throttle(40, 60_000)
  async createListingBySuperAdmin(
    @Param('adminId') adminId: string,
    @Body() dto: CreateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createListingBySuperAdmin(adminId, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Concert Guitar' },
        description: { type: 'string', example: 'Acoustic guitar in excellent condition' },
        price: { type: 'number', example: 2100 },
        currency: { type: 'string', example: 'SAR' },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        isActive: { type: 'boolean', example: true },
        condition: { type: 'string', example: 'used' },
        instrumentType: { type: 'string', example: 'Guitar' },
      },
      required: ['title', 'price', 'quantity'],
    },
  })
  @Post('superadmin/admins/:adminId/instruments')
  @Throttle(40, 60_000)
  async createInstrumentBySuperAdmin(
    @Param('adminId') adminId: string,
    @Body() dto: CreateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createInstrumentBySuperAdmin(adminId, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 8 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 8 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string', example: 'Fix Strings Workshop' },
        description: { type: 'string', example: 'Repair shop for oud and violin' },
        services: { type: 'array', items: { type: 'string' } },
        phone: { type: 'string', example: '+966500000000' },
        whatsapp: { type: 'string', example: '+966500000000' },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        location: { type: 'string', example: 'Riyadh' },
        latitude: { type: 'number', example: 24.7136 },
        longitude: { type: 'number', example: 46.6753 },
        isActive: { type: 'boolean', example: true },
      },
      required: ['name'],
    },
  })
  @Post('superadmin/admins/:adminId/repair-shops')
  @Throttle(30, 60_000)
  async createRepairShopBySuperAdmin(
    @Param('adminId') adminId: string,
    @Body() dto: CreateRepairShopDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createRepairShopBySuperAdmin(adminId, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.MARKETPLACE_MANAGE)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'shopImageFiles', maxCount: 8 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 8 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        shopName: { type: 'string', example: 'Awtarna Store' },
        shopDescription: { type: 'string', example: 'Trusted marketplace shop profile' },
        shopImageUrls: { type: 'array', items: { type: 'string' } },
        shopImageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        shopLocation: { type: 'string', example: 'Riyadh' },
        shopLatitude: { type: 'number', example: 24.7136 },
        shopLongitude: { type: 'number', example: 46.6753 },
      },
    },
  })
  @Patch('superadmin/admins/:adminId/shop-profile')
  @Throttle(30, 60_000)
  async updateShopProfileBySuperAdmin(
    @Param('adminId') adminId: string,
    @Body() dto: UpdateShopProfileDto,
    @UploadedFiles()
    files?: {
      shopImageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.updateShopProfileBySuperAdmin(
      adminId,
      dto,
      files?.shopImageFiles ?? [],
    );
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Professional Oud' },
        description: { type: 'string', example: 'Well-maintained oud for studio work' },
        price: { type: 'number', example: 3500 },
        currency: { type: 'string', example: 'SAR' },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        isActive: { type: 'boolean', example: true },
        condition: { type: 'string', example: 'used' },
        instrumentType: { type: 'string', example: 'Oud' },
        listingCategory: { type: 'string', example: 'musical_instrument' },
      },
      required: ['title', 'price', 'quantity'],
    },
  })
  @Post('admin/listings')
  @Throttle(40, 60_000)
  async createListingByAdmin(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createListingByAdmin(user.sub, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Updated listing title' },
        description: { type: 'string', example: 'Updated description' },
        price: { type: 'number', example: 3200 },
        currency: { type: 'string', example: 'SAR' },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        isActive: { type: 'boolean', example: true },
        instrumentType: { type: 'string', example: 'Oud' },
        listingCategory: { type: 'string', example: 'musical_instrument' },
      },
    },
  })
  @Patch('admin/listings/:id')
  @Throttle(60, 60_000)
  async updateListingByAdmin(
    @CurrentUser() user: JwtPayload,
    @Param('id') listingId: string,
    @Body() dto: UpdateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.updateListingByAdmin(user.sub, listingId, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Delete('admin/listings/:id')
  @Throttle(40, 60_000)
  async removeListingByAdmin(@CurrentUser() user: JwtPayload, @Param('id') listingId: string) {
    await this.marketplaceService.removeListingByAdmin(user.sub, listingId);
    return { success: true };
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Get('admin/listings/me')
  async myListings(@CurrentUser() user: JwtPayload, @Query() query: InstrumentQueryDto) {
    return this.marketplaceService.getMyListings(user.sub, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Concert Guitar' },
        description: { type: 'string', example: 'Acoustic guitar in excellent condition' },
        price: { type: 'number', example: 2100 },
        currency: { type: 'string', example: 'SAR' },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        isActive: { type: 'boolean', example: true },
        condition: { type: 'string', example: 'used' },
        instrumentType: { type: 'string', example: 'Guitar' },
      },
      required: ['title', 'price', 'quantity'],
    },
  })
  @Post('admin/instruments')
  @Throttle(40, 60_000)
  async createByAdmin(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createInstrumentByAdmin(user.sub, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 5 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 5 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        title: { type: 'string', example: 'Updated instrument title' },
        description: { type: 'string', example: 'Updated instrument description' },
        price: { type: 'number', example: 2400 },
        quantity: { type: 'number', example: 1 },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        condition: { type: 'string', example: 'used' },
        instrumentType: { type: 'string', example: 'Violin' },
      },
    },
  })
  @Patch('admin/instruments/:id')
  @Throttle(60, 60_000)
  async updateByAdmin(
    @CurrentUser() user: JwtPayload,
    @Param('id') instrumentId: string,
    @Body() dto: UpdateInstrumentDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.updateInstrumentByAdmin(
      user.sub,
      instrumentId,
      dto,
      files?.imageFiles ?? [],
    );
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Delete('admin/instruments/:id')
  @Throttle(40, 60_000)
  async removeByAdmin(@CurrentUser() user: JwtPayload, @Param('id') instrumentId: string) {
    await this.marketplaceService.removeInstrumentByAdmin(user.sub, instrumentId);
    return { success: true };
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Get('admin/instruments/me')
  async myInstruments(@CurrentUser() user: JwtPayload, @Query() query: InstrumentQueryDto) {
    return this.marketplaceService.getMyInstruments(user.sub, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 8 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 8 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string', example: 'Fix Strings Workshop' },
        description: { type: 'string', example: 'Repair shop for oud and violin' },
        services: { type: 'array', items: { type: 'string' } },
        phone: { type: 'string', example: '+966500000000' },
        whatsapp: { type: 'string', example: '+966500000000' },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        location: { type: 'string', example: 'Riyadh' },
        latitude: { type: 'number', example: 24.7136 },
        longitude: { type: 'number', example: 46.6753 },
        isActive: { type: 'boolean', example: true },
      },
      required: ['name'],
    },
  })
  @Post('admin/repair-shops')
  @Throttle(30, 60_000)
  async createRepairShop(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateRepairShopDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.createRepairShop(user.sub, dto, files?.imageFiles ?? []);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'imageFiles', maxCount: 8 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 8 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string', example: 'Updated shop name' },
        description: { type: 'string', example: 'Updated repair shop description' },
        services: { type: 'array', items: { type: 'string' } },
        phone: { type: 'string', example: '+966500000000' },
        whatsapp: { type: 'string', example: '+966500000000' },
        imageUrls: { type: 'array', items: { type: 'string' } },
        imageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        location: { type: 'string', example: 'Jeddah' },
        latitude: { type: 'number', example: 21.5433 },
        longitude: { type: 'number', example: 39.1728 },
        isActive: { type: 'boolean', example: true },
      },
    },
  })
  @Patch('admin/repair-shops/:id')
  @Throttle(40, 60_000)
  async updateRepairShop(
    @CurrentUser() user: JwtPayload,
    @Param('id') repairShopId: string,
    @Body() dto: UpdateRepairShopDto,
    @UploadedFiles()
    files?: {
      imageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.updateRepairShop(
      user.sub,
      repairShopId,
      dto,
      files?.imageFiles ?? [],
    );
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Delete('admin/repair-shops/:id')
  @Throttle(30, 60_000)
  async deleteRepairShop(@CurrentUser() user: JwtPayload, @Param('id') repairShopId: string) {
    await this.marketplaceService.removeRepairShop(user.sub, repairShopId);
    return { success: true };
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Get('admin/repair-shops/me')
  async myRepairShops(@CurrentUser() user: JwtPayload, @Query() query: RepairShopQueryDto) {
    return this.marketplaceService.getMyRepairShops(user.sub, query);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @Get('admin/shop-profile/me')
  async myShopProfile(@CurrentUser() user: JwtPayload) {
    return this.marketplaceService.getMyShopProfile(user.sub);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @UseInterceptors(FileFieldsInterceptor([{ name: 'shopImageFiles', maxCount: 8 }], { limits: { fileSize: MEDIA_MAX_SIZE_BYTES.marketplaceImage, files: 8 } }))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        shopName: { type: 'string', example: 'Awtarna Store' },
        shopDescription: { type: 'string', example: 'Trusted marketplace shop profile' },
        shopImageUrls: { type: 'array', items: { type: 'string' } },
        shopImageFiles: { type: 'array', items: { type: 'string', format: 'binary' } },
        shopLocation: { type: 'string', example: 'Riyadh' },
        shopLatitude: { type: 'number', example: 24.7136 },
        shopLongitude: { type: 'number', example: 46.6753 },
      },
    },
  })
  @Patch('admin/shop-profile')
  @Throttle(30, 60_000)
  async updateShopProfile(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdateShopProfileDto,
    @UploadedFiles()
    files?: {
      shopImageFiles?: Array<{ mimetype?: string; size: number; buffer: Buffer; originalname?: string }>;
    },
  ) {
    return this.marketplaceService.updateMyShopProfile(
      user.sub,
      dto,
      files?.shopImageFiles ?? [],
    );
  }
}
