import 'package:dartz/dartz.dart';
import '../../core/error/exceptions.dart';
import '../../core/error/failures.dart';
import '../datasources/advance_remote_data_source.dart';
import '../dto/advance_request_dto.dart';
import '../../domain/models/advance_request_model.dart';
import '../../domain/models/advances_list_response_model.dart';
import '../../domain/repositories/advance_repository.dart';

class AdvanceRepositoryImpl implements AdvanceRepository {
  final AdvanceRemoteDataSource remoteDataSource;

  AdvanceRepositoryImpl({required this.remoteDataSource});

  @override
  Future<Either<Failure, AdvanceResponseModel>> createAdvance(
    AdvanceRequestModel request,
  ) async {
    try {
      final dto = AdvanceRequestDto(
        employeeId: request.employeeId,
        date: request.date,
        amount: request.amount,
        reason: request.reason,
      );

      final responseDto = await remoteDataSource.createAdvance(dto);

      // Convert DTO to Model
      final responseModel = AdvanceResponseModel(
        statusCode: responseDto.statusCode,
        isSuccess: responseDto.isSuccess,
        message: responseDto.message,
        data: responseDto.data != null
            ? AdvanceDataModel(
                employeeId: responseDto.data!.employeeId,
                employeeFullName: responseDto.data!.employeeFullName,
                date: responseDto.data!.date,
                amount: responseDto.data!.amount,
                submittedBy: responseDto.data!.submittedBy,
                submittedByUser: responseDto.data!.submittedByUser,
                reason: responseDto.data!.reason,
                state: responseDto.data!.state,
                id: responseDto.data!.id,
                createdAt: responseDto.data!.createdAt,
                updatedAt: responseDto.data!.updatedAt,
                deletedAt: responseDto.data!.deletedAt,
                isDeleted: responseDto.data!.isDeleted,
              )
            : null,
      );

      return Right(responseModel);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException catch (e) {
      return Left(NetworkFailure(e.message));
    } catch (e) {
      return Left(ServerFailure('خطأ غير متوقع: $e'));
    }
  }

  @override
  Future<Either<Failure, AdvancesListResponseModel>> getAdvances() async {
    try {
      final responseDto = await remoteDataSource.getAdvances();

      // Convert DTO to Model
      final responseModel = AdvancesListResponseModel(
        statusCode: responseDto.statusCode,
        isSuccess: responseDto.isSuccess,
        message: responseDto.message,
        data: responseDto.data != null
            ? AdvancesListDataModel(
                items: responseDto.data!.items
                    .map((dto) => AdvanceDataModel(
                          employeeId: dto.employeeId,
                          employeeFullName: dto.employeeFullName,
                          date: dto.date,
                          amount: dto.amount,
                          submittedBy: dto.submittedBy,
                          submittedByUser: dto.submittedByUser,
                          reason: dto.reason,
                          state: dto.state,
                          id: dto.id,
                          createdAt: dto.createdAt,
                          updatedAt: dto.updatedAt,
                          deletedAt: dto.deletedAt,
                          isDeleted: dto.isDeleted,
                        ))
                    .toList(),
                pageNumber: responseDto.data!.pageNumber,
                pageSize: responseDto.data!.pageSize,
                totalCount: responseDto.data!.totalCount,
                totalPages: responseDto.data!.totalPages,
              )
            : null,
      );

      return Right(responseModel);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException catch (e) {
      return Left(NetworkFailure(e.message));
    } catch (e) {
      return Left(ServerFailure('خطأ غير متوقع: $e'));
    }
  }
}
