import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../../data/models/license/license_dto.dart';
import '../../../models/license_model.dart';
import '../../repositories/license_repository.dart';
import '../../../core/errors/failures.dart';
import '../base_usecase.dart';

/// 라이선스 수정 UseCase
@injectable
class UpdateLicenseUseCase implements UseCase<LicenseDto, UpdateLicenseParams> {
  final LicenseRepository repository;

  UpdateLicenseUseCase(this.repository);

  @override
  Future<Either<Failure, LicenseDto>> call(UpdateLicenseParams params) async {
    try {
      // 비즈니스 로직: 만료일 검증
      if (params.expiryDate != null && params.purchaseDate != null) {
        if (params.expiryDate!.isBefore(params.purchaseDate!)) {
          return Left(ValidationFailure(message: '만료일은 구매일 이후여야 합니다'));
        }

        // 비즈니스 로직: 최소 라이선스 기간 검증 (30일)
        final duration = params.expiryDate!.difference(params.purchaseDate!).inDays;
        if (duration < 30) {
          return Left(ValidationFailure(message: '라이선스 기간은 최소 30일 이상이어야 합니다'));
        }
      }

      final license = License(
        id: params.id,
        licenseKey: params.licenseKey ?? '',
        productName: params.productName ?? '',
        vendor: params.vendor,
        licenseType: params.licenseType,
        userCount: params.userCount,
        purchaseDate: params.purchaseDate ?? DateTime.now(),
        expiryDate: params.expiryDate ?? DateTime.now(),
        purchasePrice: params.purchasePrice,
        companyId: params.companyId ?? 0,
        branchId: params.branchId,
        assignedUserId: params.assignedUserId,
        remark: params.remark,
        isActive: params.isActive ?? true,
      );

      final result = await repository.updateLicense(params.id ?? 0, license);
      return result.map((updatedLicense) => LicenseDto(
        id: updatedLicense.id ?? 0,
        licenseKey: updatedLicense.licenseKey,
        productName: updatedLicense.productName,
        vendor: updatedLicense.vendor,
        licenseType: updatedLicense.licenseType,
        userCount: updatedLicense.userCount,
        purchaseDate: updatedLicense.purchaseDate,
        expiryDate: updatedLicense.expiryDate,
        purchasePrice: updatedLicense.purchasePrice,
        companyId: updatedLicense.companyId,
        branchId: updatedLicense.branchId,
        assignedUserId: updatedLicense.assignedUserId,
        remark: updatedLicense.remark,
        isActive: updatedLicense.isActive,
        createdAt: updatedLicense.createdAt ?? DateTime.now(),
        updatedAt: updatedLicense.updatedAt ?? DateTime.now(),
        companyName: updatedLicense.companyName,
        branchName: updatedLicense.branchName,
        assignedUserName: updatedLicense.assignedUserName,
      ));
    } catch (e) {
      return Left(ServerFailure(message: e.toString()));
    }
  }
}

/// 라이선스 수정 파라미터
class UpdateLicenseParams {
  final int id;
  final String? licenseKey;
  final String? productName;
  final String? vendor;
  final String? licenseType;
  final int? userCount;
  final DateTime? purchaseDate;
  final DateTime? expiryDate;
  final double? purchasePrice;
  final int? companyId;
  final int? branchId;
  final int? assignedUserId;
  final String? remark;
  final bool? isActive;

  UpdateLicenseParams({
    required this.id,
    this.licenseKey,
    this.productName,
    this.vendor,
    this.licenseType,
    this.userCount,
    this.purchaseDate,
    this.expiryDate,
    this.purchasePrice,
    this.companyId,
    this.branchId,
    this.assignedUserId,
    this.remark,
    this.isActive,
  });
}