#!/usr/bin/env python
# coding: utf-8

"""Defines the VIS pan and tilt records tables using the SQLAlchemy ORM."""

# Copyright 2023, United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
# Reuse is permitted under the terms of the license.
# The AUTHORS file and the LICENSE file are at the
# top level of this library.

from sqlalchemy import (
    DateTime,
    Float,
)
from sqlalchemy.orm import mapped_column, validates

from vipersci.vis.db import Base
import vipersci.vis.db.validators as vld


class PanRecord(Base):
    """An object to represent rows in the pan_records table for VIS.  Each row
    represents a pan angle at a particular time."""

    # This class is derived from SQLAlchemy's orm.DeclarativeBase
    # which means that it has a variety of class properties that are
    # then swept up into properties on the instantiated object via
    # super().__init__().

    # The table represents many of these objects, so the __tablename__ is
    # plural while the class name is singular.
    __tablename__ = "pan_records"

    angle = mapped_column(
        Float,
        nullable=False,
        doc="Value in radians of the pan angle of the rover PTU.  Zero is forward (+x) "
        "relative to the rover frame.",
    )
    datetime = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        primary_key=True,
        doc="The datetime at which the pan angle was reported.",
    )

    @validates("datetime")
    def validate_datetime_asutc(self, key, value):
        return vld.validate_datetime_asutc(key, value)


class TiltRecord(Base):
    """An object to represent rows in the tilt_records table for VIS.  Each row
    represents a tilt angle at a particular time."""

    # This class is derived from SQLAlchemy's orm.DeclarativeBase
    # which means that it has a variety of class properties that are
    # then swept up into properties on the instantiated object via
    # super().__init__().

    # The table represents many of these objects, so the __tablename__ is
    # plural while the class name is singular.
    __tablename__ = "tilt_records"

    angle = mapped_column(
        Float,
        nullable=False,
        doc="Value in radians of the tilt angle of the rover PTU.  Zero is level "
        "relative to the rover frame.",
    )
    datetime = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        primary_key=True,
        doc="The datetime at which the pan angle was reported.",
    )

    @validates("datetime")
    def validate_datetime_asutc(self, key, value):
        return vld.validate_datetime_asutc(key, value)
