import {
  Controller,
  Get,
  Post,
  UseGuards,
  Req,
  HttpCode,
  HttpStatus,
  Header,
  Body,
} from '@nestjs/common';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ComplianceService } from './compliance.service';
import { Request } from 'express';

@Controller('compliance')
@UseGuards(JwtAuthGuard)
export class ComplianceController {
  constructor(private readonly complianceService: ComplianceService) {}

  /**
   * Export all user data (GDPR Right to Data Portability)
   * GET /compliance/data-export
   */
  @Get('data-export')
  @HttpCode(HttpStatus.OK)
  @Header('Content-Type', 'application/json')
  @Header('Content-Disposition', 'attachment; filename="user-data-export.json"')
  async exportUserData(@Req() req: Request) {
    const userId = req.user['userId'];
    const exportData = await this.complianceService.exportUserData(userId);

    return {
      success: true,
      data: exportData,
      message: 'User data exported successfully',
    };
  }

  /**
   * Request account deletion with 30-day grace period (GDPR Right to Erasure)
   * POST /compliance/request-deletion
   */
  @Post('request-deletion')
  @HttpCode(HttpStatus.OK)
  async requestDeletion(
    @Req() req: Request,
    @Body() body: { reason?: string },
  ) {
    const userId = req.user['userId'];
    const ipAddress = req.ip;
    const userAgent = req.get('user-agent');

    const deletionRequest = await this.complianceService.requestAccountDeletion(
      userId,
      body.reason,
      ipAddress,
      userAgent,
    );

    return {
      success: true,
      data: {
        id: deletionRequest.id,
        requestedAt: deletionRequest.requestedAt,
        scheduledDeletionAt: deletionRequest.scheduledDeletionAt,
        status: deletionRequest.status,
      },
      message: `Account deletion scheduled for ${deletionRequest.scheduledDeletionAt.toLocaleDateString()}. You can cancel this request within 30 days.`,
    };
  }

  /**
   * Cancel pending account deletion request
   * POST /compliance/cancel-deletion
   */
  @Post('cancel-deletion')
  @HttpCode(HttpStatus.OK)
  async cancelDeletion(
    @Req() req: Request,
    @Body() body: { cancellationReason?: string },
  ) {
    const userId = req.user['userId'];

    const deletionRequest = await this.complianceService.cancelAccountDeletion(
      userId,
      body.cancellationReason,
    );

    return {
      success: true,
      data: {
        id: deletionRequest.id,
        cancelledAt: deletionRequest.cancelledAt,
        status: deletionRequest.status,
      },
      message: 'Account deletion request has been cancelled successfully',
    };
  }

  /**
   * Get pending deletion request status
   * GET /compliance/deletion-status
   */
  @Get('deletion-status')
  @HttpCode(HttpStatus.OK)
  async getDeletionStatus(@Req() req: Request) {
    const userId = req.user['userId'];

    const deletionRequest =
      await this.complianceService.getPendingDeletionRequest(userId);

    if (!deletionRequest) {
      return {
        success: true,
        data: null,
        message: 'No pending deletion request found',
      };
    }

    return {
      success: true,
      data: {
        id: deletionRequest.id,
        requestedAt: deletionRequest.requestedAt,
        scheduledDeletionAt: deletionRequest.scheduledDeletionAt,
        status: deletionRequest.status,
        reason: deletionRequest.reason,
      },
      message: 'Pending deletion request found',
    };
  }
}
