import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiParam } from '@nestjs/swagger';
import { WebstoreService } from './webstore.service';
import { UpdateStoreConfigDto } from './dto/update-store-config.dto';
import { CreateCategoryDto } from './dto/create-category.dto';
import { CreateItemDto } from './dto/create-item.dto';
import { PurchaseDto } from './dto/purchase.dto';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { Public } from '../../common/decorators/public.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';

@ApiTags('webstore')
@Controller()
export class WebstoreController {
  constructor(private readonly webstoreService: WebstoreService) {}

  // Admin Routes
  @Get('webstore/config')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get webstore configuration' })
  async getConfig(@CurrentTenant() licenseId: string) {
    const config = await this.webstoreService.getConfig(licenseId);
    return { config };
  }

  @Put('webstore/config')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update webstore configuration' })
  async updateConfig(
    @CurrentTenant() licenseId: string,
    @Body() dto: UpdateStoreConfigDto,
  ) {
    const config = await this.webstoreService.updateConfig(licenseId, dto);
    return { config };
  }

  @Get('webstore/categories')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all store categories' })
  async getCategories(@CurrentTenant() licenseId: string) {
    return this.webstoreService.getCategories(licenseId);
  }

  @Post('webstore/categories')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a new category' })
  async createCategory(
    @CurrentTenant() licenseId: string,
    @Body() dto: CreateCategoryDto,
  ) {
    return this.webstoreService.createCategory(licenseId, dto);
  }

  @Put('webstore/categories/:id')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update a category' })
  @ApiParam({ name: 'id', description: 'Category ID' })
  async updateCategory(
    @CurrentTenant() licenseId: string,
    @Param('id') categoryId: string,
    @Body() dto: Partial<CreateCategoryDto>,
  ) {
    return this.webstoreService.updateCategory(licenseId, categoryId, dto);
  }

  @Delete('webstore/categories/:id')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Delete a category' })
  @ApiParam({ name: 'id', description: 'Category ID' })
  async deleteCategory(
    @CurrentTenant() licenseId: string,
    @Param('id') categoryId: string,
  ) {
    await this.webstoreService.deleteCategory(licenseId, categoryId);
    return { message: 'Category deleted successfully' };
  }

  @Get('webstore/items')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all store items' })
  async getItems(@CurrentTenant() licenseId: string) {
    return this.webstoreService.getItems(licenseId);
  }

  @Post('webstore/items')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a new store item' })
  async createItem(
    @CurrentTenant() licenseId: string,
    @Body() dto: CreateItemDto,
  ) {
    return this.webstoreService.createItem(licenseId, dto);
  }

  @Put('webstore/items/:id')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update a store item' })
  @ApiParam({ name: 'id', description: 'Item ID' })
  async updateItem(
    @CurrentTenant() licenseId: string,
    @Param('id') itemId: string,
    @Body() dto: Partial<CreateItemDto>,
  ) {
    return this.webstoreService.updateItem(licenseId, itemId, dto);
  }

  @Delete('webstore/items/:id')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Delete a store item' })
  @ApiParam({ name: 'id', description: 'Item ID' })
  async deleteItem(
    @CurrentTenant() licenseId: string,
    @Param('id') itemId: string,
  ) {
    await this.webstoreService.deleteItem(licenseId, itemId);
    return { message: 'Item deleted successfully' };
  }

  @Get('webstore/transactions')
  @RequirePermission('store.manage')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all store transactions' })
  async getTransactions(@CurrentTenant() licenseId: string) {
    return this.webstoreService.getTransactions(licenseId);
  }

  // Public Routes
  @Public()
  @Get('public-store/:subdomain')
  @ApiOperation({ summary: 'Get public store information' })
  @ApiParam({ name: 'subdomain', description: 'Server subdomain' })
  async getPublicStore(@Param('subdomain') subdomain: string) {
    return this.webstoreService.getPublicStore(subdomain);
  }

  @Public()
  @Get('public-store/:subdomain/items')
  @ApiOperation({ summary: 'Get public store items' })
  @ApiParam({ name: 'subdomain', description: 'Server subdomain' })
  async getPublicItems(@Param('subdomain') subdomain: string) {
    return this.webstoreService.getPublicItems(subdomain);
  }

  @Public()
  @Post('public-store/:subdomain/purchase')
  @ApiOperation({ summary: 'Create a purchase order' })
  @ApiParam({ name: 'subdomain', description: 'Server subdomain' })
  async createPurchase(
    @Param('subdomain') subdomain: string,
    @Body() dto: PurchaseDto,
  ) {
    return this.webstoreService.createPurchase(subdomain, dto);
  }
}
