import { DataSource, Repository } from 'typeorm';
import { OrderRefund } from '../entities/order-refund.entity';
import { Order } from '../entities/order.entity';
import { CreateOrderRefundDto, UpdateOrderRefundDto } from '../schemas/order-refund.schema';
import type { z } from 'zod';

export class UserRefundsService {
  private orderRefundRepository: Repository<OrderRefund>;
  private orderRepository: Repository<Order>;

  constructor(dataSource: DataSource) {
    this.orderRefundRepository = dataSource.getRepository(OrderRefund);
    this.orderRepository = dataSource.getRepository(Order);
  }

  /**
   * 获取当前用户的退款列表
   */
  async getUserRefundsList(
    page: number,
    pageSize: number,
    keyword?: string,
    userId?: number,
    filters?: any
  ): Promise<[OrderRefund[], number]> {
    if (!userId) {
      throw new Error('用户ID不能为空');
    }

    const queryBuilder = this.orderRefundRepository
      .createQueryBuilder('refund')
      .leftJoinAndSelect('refund.order', 'order')
      .where('order.userId = :userId', { userId });

    if (keyword) {
      queryBuilder.andWhere(
        '(refund.orderNo LIKE :keyword OR refund.refundOrderNo LIKE :keyword)',
        { keyword: `%${keyword}%` }
      );
    }

    // 处理状态过滤
    if (filters && filters.state !== undefined) {
      queryBuilder.andWhere('refund.state = :state', { state: filters.state });
    }

    const [data, total] = await queryBuilder
      .orderBy('refund.createdAt', 'DESC')
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getManyAndCount();

    return [data, total];
  }

  /**
   * 根据ID获取当前用户的退款详情
   */
  async getUserRefundById(id: number, userId?: number): Promise<OrderRefund | null> {
    if (!userId) {
      throw new Error('用户ID不能为空');
    }

    const refund = await this.orderRefundRepository
      .createQueryBuilder('refund')
      .leftJoinAndSelect('refund.order', 'order')
      .where('refund.id = :id', { id })
      .getOne();

    if (!refund) {
      return null;
    }

    // 检查权限
    if (refund.order.userId !== userId) {
      throw new Error('无权访问该退款记录');
    }

    return refund;
  }

  /**
   * 为当前用户创建退款
   */
  async createUserRefund(data: z.infer<typeof CreateOrderRefundDto>, userId?: number): Promise<OrderRefund> {
    if (!userId) {
      throw new Error('用户ID不能为空');
    }

    // 验证订单是否属于当前用户
    const order = await this.orderRepository.findOne({
      where: { orderNo: data.orderNo || undefined, userId }
    });

    if (!order) {
      throw new Error('订单不存在或无权为该订单创建退款');
    }

    const refund = this.orderRefundRepository.create({
      ...data,
      createdBy: userId,
      updatedBy: userId
    });

    return await this.orderRefundRepository.save(refund);
  }

  /**
   * 更新当前用户的退款
   */
  async updateUserRefund(
    id: number,
    data: z.infer<typeof UpdateOrderRefundDto>,
    userId?: number
  ): Promise<OrderRefund | null> {
    if (!userId) {
      throw new Error('用户ID不能为空');
    }

    // 验证退款是否属于当前用户
    const existingRefund = await this.getUserRefundById(id, userId);
    if (!existingRefund) {
      return null;
    }

    const updatedRefund = this.orderRefundRepository.merge(existingRefund, {
      ...data,
      updatedBy: userId
    });

    return await this.orderRefundRepository.save(updatedRefund);
  }

  /**
   * 删除当前用户的退款
   */
  async deleteUserRefund(id: number, userId?: number): Promise<boolean> {
    if (!userId) {
      throw new Error('用户ID不能为空');
    }

    // 验证退款是否属于当前用户
    const existingRefund = await this.getUserRefundById(id, userId);
    if (!existingRefund) {
      return false;
    }

    await this.orderRefundRepository.remove(existingRefund);
    return true;
  }
}