import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../core/errors/failures.dart';
import '../../core/errors/exceptions.dart';
import '../../domain/repositories/model_repository.dart';
import '../datasources/remote/model_remote_datasource.dart';
import '../models/model/model_dto.dart';

/// Models 관리 Repository 구현체
/// 모델 정보 CRUD 작업을 처리하며 도메인 모델과 API DTO 간 변환을 담당
@Injectable(as: ModelRepository)
class ModelRepositoryImpl implements ModelRepository {
  final ModelRemoteDataSource remoteDataSource;

  ModelRepositoryImpl({required this.remoteDataSource});

  @override
  Future<Either<Failure, ModelListDto>> getModels({
    int? page,
    int? perPage,
    String? search,
    int? vendorId,
    bool? includeDeleted,
  }) async {
    try {
      final result = await remoteDataSource.getModels(
        page: page ?? 1,
        perPage: perPage ?? 10,
        search: search,
        vendorId: vendorId,
        includeDeleted: includeDeleted ?? false,
      );
      
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 목록 조회 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, ModelDto>> getModelDetail(int id) async {
    try {
      final result = await remoteDataSource.getModelDetail(id);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 상세 조회 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, ModelDto>> createModel(CreateModelRequest request) async {
    try {
      final result = await remoteDataSource.createModel(request);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 생성 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, ModelDto>> updateModel(int id, UpdateModelRequest request) async {
    try {
      final result = await remoteDataSource.updateModel(id, request);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 수정 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, void>> deleteModel(int id) async {
    try {
      await remoteDataSource.deleteModel(id);
      return const Right(null);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 삭제 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, ModelDto>> restoreModel(int id) async {
    try {
      final result = await remoteDataSource.restoreModel(id);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '모델 복구 실패: $e'));
    }
  }

  @override
  Future<Either<Failure, List<ModelDto>>> getModelsByVendor(int vendorId) async {
    try {
      final result = await remoteDataSource.getModelsByVendor(vendorId);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));
    } catch (e) {
      return Left(ServerFailure(message: '제조사별 모델 조회 실패: $e'));
    }
  }
}