import graphene
from graphene.types.scalars import Scalar

# ---------------------------------------------------------------------------
# Custom Upload Scalar
# ---------------------------------------------------------------------------

class Upload(Scalar):
    """Custom scalar type representing form file upload."""
    @staticmethod
    def serialize(value):
        return value

    @staticmethod
    def parse_literal(node):
        return node

    @staticmethod
    def parse_value(value):
        return value


# ---------------------------------------------------------------------------
# Object Types
# ---------------------------------------------------------------------------

class Scheduler(graphene.ObjectType):
    """Mirrors PHP SchedulerType fields."""
    id = graphene.Int()
    date = graphene.String()
    time = graphene.String()
    home = graphene.String()
    away = graphene.String()
    location = graphene.String()
    division = graphene.String()
    created_at = graphene.String()
    updated_at = graphene.String()


class PaginatedScheduler(graphene.ObjectType):
    """Wraps a paginated list of scheduler entries."""
    data = graphene.List(Scheduler)
    total = graphene.Int()
    page = graphene.Int()
    per_page = graphene.Int()


class DeleteResult(graphene.ObjectType):
    """Result of a delete operation."""
    success = graphene.Boolean()
    message = graphene.String()


class ImportLeagueSetupResult(graphene.ObjectType):
    """Result of a league setup import operation."""
    success = graphene.Boolean()
    message = graphene.String()


class Season(graphene.ObjectType):
    """Represents a season in the league."""
    season_id = graphene.Int()
    season_desc = graphene.String()
    year_id = graphene.Int()


class Division(graphene.ObjectType):
    """Represents a division in the league."""
    division_id = graphene.Int()
    division_name = graphene.String()


class CreateSeasonResult(graphene.ObjectType):
    """Result of a season creation operation."""
    success = graphene.Boolean()
    message = graphene.String()
    season_id = graphene.Int()


class CreateDivisionResult(graphene.ObjectType):
    """Result of a division creation operation."""
    success = graphene.Boolean()
    message = graphene.String()
    division_id = graphene.Int()


class CreateFieldResult(graphene.ObjectType):
    """Result of a field creation operation."""
    success = graphene.Boolean()
    message = graphene.String()
    field_id = graphene.Int()


class CreateTeamResult(graphene.ObjectType):
    """Result of a team creation operation."""
    success = graphene.Boolean()
    message = graphene.String()
    team_id = graphene.Int()


# ---------------------------------------------------------------------------
# Input Types
# ---------------------------------------------------------------------------

class CreateSchedulerInput(graphene.InputObjectType):
    """Input for createScheduler mutation."""
    date = graphene.String(required=True)
    time = graphene.String(required=True)
    home = graphene.String(required=True)
    away = graphene.String(required=True)
    location = graphene.String(required=True)
    division = graphene.String(required=True)


class UpdateSchedulerInput(graphene.InputObjectType):
    """Input for updateScheduler mutation — all fields optional."""
    id = graphene.Int(required=True)
    date = graphene.String()
    time = graphene.String()
    home = graphene.String()
    away = graphene.String()
    location = graphene.String()
    division = graphene.String()


class CreateSeasonInput(graphene.InputObjectType):
    """Input for createSeason mutation."""
    season_name = graphene.String(required=True)
    start_date = graphene.Date(required=True)
    end_date = graphene.Date(required=True)
    season_year = graphene.Int(required=True)
    status = graphene.String(default_value="active")
    registration_start = graphene.Date()
    registration_end = graphene.Date()
    early_bird_date = graphene.Date()
    late_fee_date = graphene.Date()


class CreateDivisionInput(graphene.InputObjectType):
    """Input for createDivision mutation."""
    division_name = graphene.String(required=True)
    gender = graphene.String(required=True)
    start_age_or_grade = graphene.Float(default_value=0.0)
    end_age_or_grade = graphene.Float(default_value=0.0)
    max_teams = graphene.Int(default_value=0)


class CreateFieldInput(graphene.InputObjectType):
    """Input for createField mutation."""
    field_name = graphene.String(required=True)
    address = graphene.String()


class CreateTeamInput(graphene.InputObjectType):
    """Input for createTeam mutation."""
    team_name = graphene.String(required=True)
    division_id = graphene.Int(required=True)
    home_field_id = graphene.Int(required=True)
    affiliation = graphene.String()
    season_id = graphene.Int()
