"""SciNote rest api client for inventory cells."""

import requests

from typing import List
from pydantic import ValidationError

from ..models.inventory_cell import (
    InventoryCell,
    CreateInventoryCell,
    UpdateInventoryCell,
)


class InventoryCellClient:
    """Client for interacting with the SciNote REST API."""

    def __init__(
        self,
        base_url: str,
        api_key: str,
        team_id: int,
        inventory_id: int,
        item: int,
    ):
        """Initialize the client."""
        self.base_url = base_url
        self.api_key = api_key
        self.team_id = team_id
        self.inventory_id = inventory_id
        self.item = item
        self.headers = {
            'Api-Key': api_key,
        }

    def get_cells(self) -> List[InventoryCell]:
        """Get all inventory cells from SciNote."""

        # Need to loop through all pages to get all of the columns
        url = (
            f'{self.base_url}/api/v1/teams/{self.team_id}/inventories/'
            f'{self.inventory_id}/items/{self.item}/cells'
        )
        result = []

        # Keep track of visited urls to avoid infinite loops
        visited = set()
        while url:
            response = requests.get(url, headers=self.headers)
            response.raise_for_status()
            visited.add(url)
            try:
                [
                    result.append(InventoryCell(**column))
                    for column in response.json()['data']
                ]
            except ValidationError as e:
                raise ValueError(f'Invalid response from SciNote: {e}')

            url = response.json()['links'].get('next', None)

            # Check we haven't visited this url before
            if url in visited:
                break

        return result

    def create_cell(self, cell: CreateInventoryCell) -> str:
        """Create a new inventory cell in SciNote."""
        url = (
            f'{self.base_url}/api/v1/teams/{self.team_id}/inventories/'
            f'{self.inventory_id}/items/{self.item}/cells'
        )
        response = requests.post(
            url,
            headers=self.headers,
            json={
                'data': {
                    'type': cell.type,
                    'attributes': {
                        'value': cell.value,
                        'column_id': cell.column_id,
                    },
                },
            },
        )
        response.raise_for_status()
        return response.json()['data']['id']

    def update_cell(self, cell: UpdateInventoryCell) -> InventoryCell:
        """Update an inventory cell in SciNote."""
        url = (
            f'{self.base_url}/api/v1/teams/{self.team_id}/inventories/'
            f'{self.inventory_id}/items/{self.item}/cells/{cell.id}'
        )
        response = requests.patch(
            url,
            headers=self.headers,
            json={
                'data': {
                    'id': cell.id,
                    'type': cell.type,
                    'attributes': {
                        'value': cell.value,
                        'column_id': cell.column_id,
                    },
                },
            },
        )
        response.raise_for_status()
        # If there was no update then the server will return a 204 with no
        # body, so we will return None in that case.
        if response.status_code == 204:
            return None
        else:
            return InventoryCell(**response.json()['data'])
