import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../../data/models/warehouse/warehouse_dto.dart';
import '../../../models/warehouse_location_model.dart';
import '../../../models/address_model.dart';
import '../../repositories/warehouse_location_repository.dart';
import '../../../core/errors/failures.dart';
import '../base_usecase.dart';

/// 창고 위치 수정 UseCase
@injectable
class UpdateWarehouseLocationUseCase implements UseCase<WarehouseLocationDto, UpdateWarehouseLocationParams> {
  final WarehouseLocationRepository repository;

  UpdateWarehouseLocationUseCase(this.repository);

  @override
  Future<Either<Failure, WarehouseLocationDto>> call(UpdateWarehouseLocationParams params) async {
    try {
      // 비즈니스 로직: 이름 유효성 검증
      if (params.name != null && params.name!.isEmpty) {
        return Left(ValidationFailure(message: '창고 위치 이름은 비어있을 수 없습니다'));
      }

      // 비즈니스 로직: 주소 유효성 검증
      if (params.address != null && params.address!.isEmpty) {
        return Left(ValidationFailure(message: '창고 주소는 비어있을 수 없습니다'));
      }

      // 비즈니스 로직: 연락처 형식 검증
      if (params.contactNumber != null && params.contactNumber!.isNotEmpty) {
        final phoneRegex = RegExp(r'^[\d\-\+\(\)\s]+$');
        if (!phoneRegex.hasMatch(params.contactNumber!)) {
          return Left(ValidationFailure(message: '올바른 연락처 형식이 아닙니다'));
        }
      }

      // 비즈니스 로직: 좌표 유효성 검증
      if (params.latitude != null && (params.latitude! < -90 || params.latitude! > 90)) {
        return Left(ValidationFailure(message: '유효하지 않은 위도값입니다'));
      }
      if (params.longitude != null && (params.longitude! < -180 || params.longitude! > 180)) {
        return Left(ValidationFailure(message: '유효하지 않은 경도값입니다'));
      }

      final warehouseLocation = WarehouseLocation(
        id: params.id,
        name: params.name ?? '',
        address: params.address,
        remark: params.description,
      );

      final result = await repository.updateWarehouseLocation(params.id, warehouseLocation);
      return result.map((updatedLocation) => WarehouseLocationDto(
        id: updatedLocation.id ?? 0,
        name: updatedLocation.name,
        address: updatedLocation.address,
        isActive: true, // Default value since model doesn't have isActive
        createdAt: DateTime.now(), // Add required createdAt parameter
      ));
    } catch (e) {
      return Left(ServerFailure(message: e.toString()));
    }
  }
}

/// 창고 위치 수정 파라미터
class UpdateWarehouseLocationParams {
  final int id;
  final String? name;
  final String? address;
  final String? description;
  final String? contactNumber;
  final String? manager;
  final double? latitude;
  final double? longitude;
  final bool? isActive;

  UpdateWarehouseLocationParams({
    required this.id,
    this.name,
    this.address,
    this.description,
    this.contactNumber,
    this.manager,
    this.latitude,
    this.longitude,
    this.isActive,
  });

  Map<String, dynamic> toMap() {
    final Map<String, dynamic> data = {};
    if (name != null) data['name'] = name;
    if (address != null) data['address'] = address;
    if (description != null) data['description'] = description;
    if (contactNumber != null) data['contact_number'] = contactNumber;
    if (manager != null) data['manager'] = manager;
    if (latitude != null) data['latitude'] = latitude;
    if (longitude != null) data['longitude'] = longitude;
    if (isActive != null) data['is_active'] = isActive;
    return data;
  }
}