import 'package:dartz/dartz.dart';
import '../../../services/company_service.dart';
import '../../../models/company_model.dart';
import '../../../core/errors/failures.dart';
import '../base_usecase.dart';

/// 회사 상세 조회 파라미터
class GetCompanyDetailParams {
  final int id;
  final bool includeBranches;

  const GetCompanyDetailParams({
    required this.id,
    this.includeBranches = false,
  });
}

/// 회사 상세 조회 UseCase
class GetCompanyDetailUseCase extends UseCase<Company, GetCompanyDetailParams> {
  final CompanyService _companyService;

  GetCompanyDetailUseCase(this._companyService);

  @override
  Future<Either<Failure, Company>> call(GetCompanyDetailParams params) async {
    try {
      final Company company;
      
      if (params.includeBranches) {
        company = await _companyService.getCompanyWithChildren(params.id);
      } else {
        company = await _companyService.getCompanyDetail(params.id);
      }
      
      return Right(company);
    } on ServerFailure catch (e) {
      if (e.message.contains('not found')) {
        return Left(ValidationFailure(
          message: '회사를 찾을 수 없습니다.',
          code: 'NOT_FOUND',
          originalError: e,
        ));
      }
      return Left(ServerFailure(
        message: e.message,
        originalError: e,
      ));
    } catch (e) {
      return Left(UnknownFailure(
        message: '회사 정보를 불러오는 중 오류가 발생했습니다.',
        originalError: e,
      ));
    }
  }
}