import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { IssuerStoreService } from '../../../application/services/issuer-store.service';
import { CreateStoreDto, UpdateStoreDto, ListStoresQueryDto } from '../dto/store.dto';
import { CreateEmployeeDto, UpdateEmployeeDto, ListEmployeesQueryDto } from '../dto/employee.dto';

// ---- Store Controller ----

@ApiTags('Issuer Stores')
@Controller('issuers/me/stores')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class IssuerStoreController {
  constructor(private readonly issuerStoreService: IssuerStoreService) {}

  @Get()
  @ApiOperation({ summary: 'List stores for the current issuer' })
  async listStores(@Req() req: any, @Query() query: ListStoresQueryDto) {
    const stores = await this.issuerStoreService.listStores(req.user.issuerId, {
      level: query.level,
      parentId: query.parentId,
      status: query.status,
    });
    return { code: 0, data: stores };
  }

  @Post()
  @ApiOperation({ summary: 'Create a new store' })
  async createStore(@Req() req: any, @Body() dto: CreateStoreDto) {
    const store = await this.issuerStoreService.createStore(req.user.issuerId, {
      name: dto.name,
      address: dto.address || null,
      phone: dto.phone || null,
      level: dto.level || 'store',
      parentId: dto.parentId || null,
    });
    return { code: 0, data: store };
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update a store' })
  async updateStore(
    @Req() req: any,
    @Param('id') id: string,
    @Body() dto: UpdateStoreDto,
  ) {
    const store = await this.issuerStoreService.updateStore(req.user.issuerId, id, dto);
    return { code: 0, data: store };
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a store' })
  async deleteStore(@Req() req: any, @Param('id') id: string) {
    await this.issuerStoreService.deleteStore(req.user.issuerId, id);
    return { code: 0, message: 'Store deleted' };
  }
}

// ---- Employee Controller ----

@ApiTags('Issuer Employees')
@Controller('issuers/me/employees')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class IssuerEmployeeController {
  constructor(private readonly issuerStoreService: IssuerStoreService) {}

  @Get()
  @ApiOperation({ summary: 'List employees for the current issuer' })
  async listEmployees(@Req() req: any, @Query() query: ListEmployeesQueryDto) {
    const employees = await this.issuerStoreService.listEmployees(req.user.issuerId, {
      storeId: query.storeId,
      role: query.role,
    });
    return { code: 0, data: employees };
  }

  @Post()
  @ApiOperation({ summary: 'Create a new employee' })
  async createEmployee(@Req() req: any, @Body() dto: CreateEmployeeDto) {
    const employee = await this.issuerStoreService.createEmployee(req.user.issuerId, {
      name: dto.name,
      phone: dto.phone || null,
      role: dto.role || 'staff',
      storeId: dto.storeId || null,
    });
    return { code: 0, data: employee };
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update an employee' })
  async updateEmployee(
    @Req() req: any,
    @Param('id') id: string,
    @Body() dto: UpdateEmployeeDto,
  ) {
    const employee = await this.issuerStoreService.updateEmployee(req.user.issuerId, id, dto);
    return { code: 0, data: employee };
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete an employee' })
  async deleteEmployee(@Req() req: any, @Param('id') id: string) {
    await this.issuerStoreService.deleteEmployee(req.user.issuerId, id);
    return { code: 0, message: 'Employee deleted' };
  }
}
