import Foundation

struct AuthResponse: Codable {
    struct User: Codable {
        let id: String
        let email: String
    }

    let token: String
    let user: User
}

protocol AuthServiceProtocol {
    func login(email: String, password: String) async throws -> AuthResponse
    func register(email: String, password: String) async throws -> AuthResponse
    func profile(token: String) async throws -> UserProfile
}

final class AuthService: AuthServiceProtocol {
    private let client: APIClientProtocol

    init(client: APIClientProtocol) {
        self.client = client
    }

    func login(email: String, password: String) async throws -> AuthResponse {
        try await client.post(path: "api/auth/login", body: Credentials(email: email, password: password), token: nil)
    }

    func register(email: String, password: String) async throws -> AuthResponse {
        try await client.post(path: "api/auth/register", body: Credentials(email: email, password: password), token: nil)
    }

    func profile(token: String) async throws -> UserProfile {
        try await client.get(path: "api/auth/profile", queryItems: [], token: token)
    }
}

private struct Credentials: Codable {
    let email: String
    let password: String
}
