# retoor <retoor@molodetz.nl>

from __future__ import annotations

from ..spec import Action, Param
from ._shared import body, confirm, path, query


GAME_ACTIONS: tuple[Action, ...] = (
    Action(
        name="game_state",
        method="GET",
        path="/game/state",
        summary="Get the current user's Code Farm state",
        description=(
            "Returns JSON for the signed-in user's Code Farm: coins, level/XP, CI tier, "
            "plot grid (each plot's state, crop, remaining build seconds), and the plantable crops."
        ),
        handler="http",
        requires_auth=True,
        read_only=True,
    ),
    Action(
        name="game_leaderboard",
        method="GET",
        path="/game/leaderboard",
        summary="List the top Code Farm players on a given board",
        description=(
            "Boards: score (default, overall composite score), prestige, harvests "
            "(this week), raids (avg coins per successful raid, min 3 raids), "
            "time_to_kernel (fastest since last refactor), fair_play (rewards recent "
            "activity over hoarding), era (current Era-only leaderboard, empty when no "
            "Era is running)."
        ),
        handler="http",
        requires_auth=False,
        read_only=True,
        params=(query("board", "Leaderboard board key, defaults to score."),),
    ),
    Action(
        name="game_view_farm",
        method="GET",
        path="/game/farm/{username}",
        summary="View another player's Code Farm",
        handler="http",
        requires_auth=False,
        read_only=True,
        params=(path("username", "The farm owner's username."),),
    ),
    Action(
        name="game_plant",
        method="POST",
        path="/game/plant",
        summary="Plant a crop in an empty plot on your Code Farm",
        handler="http",
        requires_auth=True,
        params=(
            Param(name="slot", location="body", description="Plot slot index.", required=True, type="integer"),
            body(
                "crop",
                "Crop key: shell, python, webapp, api, rust, haskell, kernel, or "
                "(with Mastery) distsys, mlpipe, secfort.",
                required=True,
            ),
        ),
    ),
    Action(
        name="game_harvest",
        method="POST",
        path="/game/harvest",
        summary="Harvest a finished build from a plot on your Code Farm",
        handler="http",
        requires_auth=True,
        params=(
            Param(name="slot", location="body", description="Plot slot index.", required=True, type="integer"),
        ),
    ),
    Action(
        name="game_buy_plot",
        method="POST",
        path="/game/buy-plot",
        summary="Buy a new plot on your Code Farm",
        handler="http",
        requires_auth=True,
    ),
    Action(
        name="game_upgrade_ci",
        method="POST",
        path="/game/upgrade",
        summary="Upgrade your Code Farm CI tier for faster builds",
        handler="http",
        requires_auth=True,
    ),
    Action(
        name="game_water",
        method="POST",
        path="/game/farm/{username}/water",
        summary="Water a growing build on another player's Code Farm",
        handler="http",
        requires_auth=True,
        params=(
            path("username", "The farm owner's username."),
            Param(name="slot", location="body", description="Plot slot index.", required=True, type="integer"),
        ),
    ),
    Action(
        name="game_steal",
        method="POST",
        path="/game/farm/{username}/steal",
        summary="Steal a ready build from another player's Code Farm once its protection window has passed (limited to once per hour per neighbour)",
        handler="http",
        requires_auth=True,
        params=(
            path("username", "The farm owner's username."),
            Param(name="slot", location="body", description="Plot slot index.", required=True, type="integer"),
        ),
    ),
    Action(
        name="game_fertilize",
        method="POST",
        path="/game/fertilize",
        summary="Spend coins to halve a growing build's remaining time on your Code Farm (cost scales with the build's harvest value, so it is a pure time-skip, never a profit)",
        handler="http",
        requires_auth=True,
        params=(
            Param(name="slot", location="body", description="Plot slot index.", required=True, type="integer"),
        ),
    ),
    Action(
        name="game_daily",
        method="POST",
        path="/game/daily",
        summary="Claim the daily Code Farm coin bonus (builds a streak)",
        handler="http",
        requires_auth=True,
    ),
    Action(
        name="game_upgrade_perk",
        method="POST",
        path="/game/perk",
        summary="Upgrade a permanent Code Farm perk (yield, growth, discount, xp)",
        handler="http",
        requires_auth=True,
        params=(
            body("perk", "Perk key: yield, growth, discount, or xp.", required=True),
        ),
    ),
    Action(
        name="game_claim_quest",
        method="POST",
        path="/game/quests/claim",
        summary="Claim a completed daily or weekly Code Farm quest/contract reward",
        handler="http",
        requires_auth=True,
        params=(
            body("quest", "Quest kind: plant, harvest, water, or earn.", required=True),
            body(
                "scope",
                "daily (default) or weekly (requires the Legacy Contracts Mastery upgrade).",
                required=False,
            ),
        ),
    ),
    Action(
        name="game_prestige",
        method="POST",
        path="/game/prestige",
        summary=(
            "Refactor (prestige) your Code Farm for a permanent coin bonus and Stars. "
            "Requires level 10 AND a coin fee that scales with prestige and wealth "
            "(shown as refactor_cost in game_state); the fee funds the community "
            "treasury and a fraction of the remaining coins carries over. "
            "Irreversible, confirmation required"
        ),
        handler="http",
        requires_auth=True,
        params=(confirm(),),
    ),
    Action(
        name="game_claim_grant",
        method="POST",
        path="/game/grant",
        summary=(
            "Claim the weekly Code Farm community grant, paid from the treasury of "
            "refactor fees (active low-balance, low-prestige farms only)"
        ),
        handler="http",
        requires_auth=True,
    ),
    Action(
        name="game_upgrade_legacy",
        method="POST",
        path="/game/legacy",
        summary="Spend Stars on a permanent Legacy upgrade that survives refactor (autoharvest, multiplier, speed, plots, defense, carryover)",
        handler="http",
        requires_auth=True,
        params=(
            body(
                "key",
                "Legacy key: autoharvest, multiplier, speed, plots, defense, or carryover.",
                required=True,
            ),
            confirm(),
        ),
    ),
    Action(
        name="game_upgrade_mastery",
        method="POST",
        path="/game/mastery",
        summary="Spend Mastery points on a permanent Mastery upgrade (unlocked at prestige 50+): autoreplant, analytics, or contracts",
        handler="http",
        requires_auth=True,
        params=(
            body("key", "Mastery key: autoreplant, analytics, or contracts.", required=True),
            confirm(),
        ),
    ),
    Action(
        name="game_buy_infrastructure",
        method="POST",
        path="/game/infrastructure/buy",
        summary="Buy a permanent Infrastructure building (registry, canary, observability); expensive, prestige-gated, big coin sinks",
        handler="http",
        requires_auth=True,
        params=(
            body("key", "Infrastructure key: registry, canary, or observability.", required=True),
            confirm(),
        ),
    ),
    Action(
        name="game_upgrade_defense",
        method="POST",
        path="/game/defense/upgrade",
        summary=(
            "Upgrade your farm's Defense tier: reduces raid losses and adds steal grace, "
            "but commits you to an ongoing daily coin upkeep proportional to your wealth. "
            "Irreversible spend, confirmation required"
        ),
        handler="http",
        requires_auth=True,
        params=(confirm(),),
    ),
    Action(
        name="game_downgrade_defense",
        method="POST",
        path="/game/defense/downgrade",
        summary=(
            "Drop your farm's Defense down one tier to escape its daily upkeep. "
            "No refund, confirmation required"
        ),
        handler="http",
        requires_auth=True,
        params=(confirm(),),
    ),
    Action(
        name="game_buy_cosmetic",
        method="POST",
        path="/game/cosmetics/buy",
        summary="Buy a purely cosmetic title or plot skin with coins (no gameplay effect)",
        handler="http",
        requires_auth=True,
        params=(
            body("key", "Cosmetic key from the farm's cosmetics list.", required=True),
            confirm(),
        ),
    ),
    Action(
        name="game_equip_cosmetic",
        method="POST",
        path="/game/cosmetics/equip",
        summary="Equip an owned cosmetic title so it shows on the leaderboard",
        handler="http",
        requires_auth=True,
        params=(body("key", "An owned title cosmetic key.", required=True),),
    ),
)
