# coding: utf-8
"""
class StationmetaCore (Base)
============================
"""
from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, String, \
                       Text, CheckConstraint, Sequence
from geoalchemy2.types import Geometry
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
from shapely import wkt
from toardb.auth_user.models import AuthUser
from toardb.base import Base

STATIONMETA_CORE_ID_SEQ = Sequence('stationmeta_core_id_seq')  # define sequence explicitly
class StationmetaCore(Base):
    """ Table "public.stationmeta_core"

    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    |            Column            |           Type           | Collation | Nullable |                   Default                    |
    +==============================+==========================+===========+==========+==============================================+
    | id                           | integer                  |           | not null | nextval('stationmeta_core_id_seq'::regclass) |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | codes                        | character varying(16)[]  |           |          |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | name                         | character varying(128)   |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | coordinates                  | geometry(PointZ,4326)    |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | country                      | character varying(128)   |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | state                        | character varying(128)   |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | coordinate_validation_status | integer                  |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | coordinate_validation_date   | timestamp with time zone |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | type_of_environment          | integer                  |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | type_of_area                 | integer                  |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | category                     | character varying(128)   |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | timezone                     | character varying(64)    |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | additional_metadata          | jsonb                    |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    | coordinate_validator_id      | integer                  |           | not null |                                              |
    +------------------------------+--------------------------+-----------+----------+----------------------------------------------+
    Indexes:
        "stationmeta_core_pkey" PRIMARY KEY, btree (id)
        "stationmeta_core_coordinate_validator_id_38c0ef8d" btree (coordinate_validator_id)
        "stationmeta_core_coordinates_id" gist (coordinates gist_geometry_ops_nd)
        "stationmeta_core_country_fa755dde" btree (country)
        "stationmeta_core_country_fa755dde_like" btree (country varchar_pattern_ops)
        "stationmeta_core_state_85025a96" btree (state)
        "stationmeta_core_state_85025a96_like" btree (state varchar_pattern_ops)
    Check constraints:
        "stationmeta_core_coordinate_validation_status_check" CHECK (coordinate_validation_status >= 0)
        "stationmeta_core_type_of_area_check" CHECK (type_of_area >= 0)
        "stationmeta_core_type_of_environment_check" CHECK (type_of_environment >= 0)
    Foreign-key constraints:
        "stationmeta_core_coordinate_validator_38c0ef8d_fk_auth_user" FOREIGN KEY (coordinate_validator_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
    Referenced by:
        TABLE "station_roles" CONSTRAINT "station_roles_station_id_f31f80fc_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "stationmeta_annotations" CONSTRAINT "stationmeta_annotati_station_id_9d3fe3d0_fk_stationme" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "stationmeta_aux_doc" CONSTRAINT "stationmeta_aux_doc_station_id_17bdb5f2_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "stationmeta_aux_image" CONSTRAINT "stationmeta_aux_imag_station_id_fbfbdb29_fk_stationme" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "stationmeta_aux_url" CONSTRAINT "stationmeta_aux_url_station_id_727571bd_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "stationmeta_global" CONSTRAINT "stationmeta_global_station_id_29ff53dd_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
        TABLE "timeseries" CONSTRAINT "timeseries_station_id_0f4fed9c_fk_stationmeta_core_id" FOREIGN KEY (station_id) REFERENCES stationmeta_core(id) DEFERRABLE INITIALLY DEFERRED
    """

    __tablename__ = 'stationmeta_core'
    __table_args__ = (
                        CheckConstraint('coordinate_validation_status >= 0'),
                        CheckConstraint('type_of_area >= 0'),
                        CheckConstraint('type_of_environment >= 0')
                     )

    id = Column(Integer, STATIONMETA_CORE_ID_SEQ, primary_key=True, server_default=STATIONMETA_CORE_ID_SEQ.next_value())
    codes = Column(ARRAY(String(16)))
    name = Column(String(128), nullable=False)
    coordinates = Column(Geometry('POINTZ', 4326))
    country = Column(String(128), nullable=False, index=True)
    state = Column(String(128), nullable=False, index=True)
    coordinate_validation_status = Column(Integer, nullable=False)
    coordinate_validation_date = Column(DateTime(True), nullable=False)
    type_of_environment = Column(Integer, nullable=False)
    type_of_area = Column(Integer, nullable=False)
    category = Column(String(128), nullable=False)
    timezone = Column(String(64), nullable=False)
    additional_metadata = Column(JSONB(astext_type=Text()), nullable=True)
    coordinate_validator_id = Column(ForeignKey(AuthUser.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True)
    #how to reactivate the following?
#   coordinate_validator = relationship('AuthUser')