//
//  FactsFeature.swift
//  Meow
//

import Foundation
import Core
import CatFactsKit
import ComposableArchitecture

@Reducer
public struct FactsFeature {
    @ObservationIgnored
    @Dependency(\.clientFactory) var clientFactory

    @ObservableState
    public struct State {
        public var baseURL: URL
        public var mode: Mode = .notLoaded

        public init(baseURL: URL = URL(string: "https://invalid.barf")!) {
            self.baseURL = baseURL
        }
    }

    public enum Mode {
        case notLoaded
        case loading
        case loaded(facts: [String])
        case error(Error)
    }

    public enum Action {
        case viewAppeared
        case receivedFacts([String])
        case receivedError(Error)
    }
    
    public init() { }

    public var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            case .viewAppeared:
                state.mode = .loading
                let client = clientFactory.createClient(baseURL: state.baseURL)
                return .run { send in
                    do {
                        let results = try await client.getFacts(count: 5)
                        await send(.receivedFacts(results))
                    } catch {
                        await send(.receivedError(error))
                    }
                }
            case let .receivedFacts(facts):
                state.mode = .loaded(facts: facts)
                return .none
            case let .receivedError(error):
                state.mode = .error(error)
                return .none
            }
        }
    }
}
