"""Shapes with areas (the classes corpus, in Vibe)."""

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Protocol

from verada import Annotated, Interval, Ordered, ensures, old, requires, result

type Length = Annotated[int, Interval(ge=0, le=1000)]
type Offset = Annotated[int, Interval(ge=-1000, le=1000)]


class Shape(ABC):
    @abstractmethod
    def area(self) -> int: ...

    def describe(self) -> str:
        return "a shape"


@dataclass
class Square(Shape):
    side: Length

    def area(self) -> int:
        ensures(result >= 0)
        return self.side * self.side


@dataclass
class Rectangle(Shape):
    width: Length
    height: Length

    def area(self) -> int:
        ensures(result >= 0)
        return self.width * self.height

    # wider by some amount, up to the longest a side may be
    def widen(self, by: Length) -> None:
        if self.width <= 1000 - by:
            self.width = self.width + by
        else:
            self.width = 1000


# a point on a grid, not a shape
@dataclass
class Point:
    x: Offset
    y: Offset

    def manhattan(self) -> int:
        ensures(0 <= result <= 2000)
        return abs(self.x) + abs(self.y)


# whichever covers more (any shape: the call dispatches)
def bigger(a: Shape, b: Shape) -> bool:
    return a.area() > b.area()


# a union: a token is a number or an operator
@dataclass
class Number:
    value: Offset


@dataclass
class Operator:
    precedence: Length


type Token = Number | Operator


# a generic class: a package of its class and methods, an instance for each of its uses
@dataclass
class Box[T]:
    value: T
    count: int = 0

    def get(self) -> T:
        return self.value

    def put(self, v: T) -> None:
        requires(self.count < 1000)
        self.value = v
        self.count = self.count + 1


def describe_token(t: Token) -> str:
    match t:
        case Number(value=v):
            if v < 0:
                return "a negative number"
            return "a number"
        case Operator(precedence=p):
            return f"an operator of precedence {p}"
        case _:
            return "something else"


# either of two values of any one type (a generic function: each call makes an instance)
def pick[T](first: bool, a: T, b: T) -> T:
    if first:
        return a
    return b


# the larger of two values of a type with an order
def larger[T: Ordered](a: T, b: T) -> T:
    return b if a < b else a


# a list of the module's own class (an instance the module declares after the class)
def tiles(side: Length) -> int:
    ensures(result == 2)
    row: list[Point] = [Point(x=side, y=0), Point(x=0, y=side)]
    return len(row)


def unboxed(b: Box[int]) -> int:
    return b.get()


# a name for an element (design D): its changes reach the list, as Python's shared object's do
def move_first(row: list[Point], x: Offset) -> None:
    requires(len(row) > 0)
    ensures(len(row) == old(len(row)))
    first = row[0]
    first.x = x
    first.y = 0


# a protocol: an interface, which a class implements by naming it
class Labelled(Protocol):
    def label(self) -> int: ...


@dataclass
class Ticket(Labelled):
    code: int

    def label(self) -> int:
        return self.code


@dataclass
class Badge(Labelled):
    level: Length

    def label(self) -> int:
        return self.level * 10


# any labelled value (the interface's class-wide type)
def label_of(x: Labelled) -> int:
    return x.label()


# a protocol as a bound: its methods are the generic's formal subprograms
def larger_label[T: Labelled](a: T, b: T) -> int:
    if a.label() < b.label():
        return b.label()
    return a.label()
