# Copyright (c) 2025 OpenStack Foundation
#
# 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.

from sqlalchemy import Column, Enum, String, Boolean, Integer, DateTime, Index
from sqlalchemy.orm import declarative_base
from oslo_db.sqlalchemy import models as oslo_models
import enum

# Create declarative base
Base = declarative_base()

class TaskState(enum.Enum):
    SCHEDULED = "SCHEDULED"
    ERROR = "ERROR"
    RUNNING = "RUNNING"
    DONE = "DONE"

class TaskType(enum.Enum):
    VALIDATE_COMPUTE = "VALIDATE_COMPUTE"
    EVACUATE = "EVACUATE"
    PROJECT_PURGE = "PROJECT_PURGE"

class VigieTasks(oslo_models.TimestampMixin, Base):
    __tablename__ = 'tasks'

    id = Column(String(36), primary_key=True)
    project_id = Column(String(36), nullable=False)
    project_name = Column(String(255), nullable=False)
    compute_hostname = Column(String(255), nullable=False)
    scheduled_time = Column(DateTime, nullable=True)
    state = Column(Enum(TaskState), 
                   default=TaskState.SCHEDULED,
                   nullable=False)
    task_type = Column(Enum(TaskType), 
                       default=TaskType.EVACUATE,
                       nullable=False)

    def to_dict(self):
        return {
            'id': self.id,
            'project_id': self.project_id,
            'project_name': self.project_name,
            'compute_hostname': self.compute_hostname,
            'scheduled_time': self.scheduled_time.isoformat() if self.scheduled_time else None,
            'state': self.state.value if self.state else None,
            'task_type': self.task_type.value if self.task_type else None,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }


class Service(oslo_models.TimestampMixin, Base):
    """Represents a running service on a host."""

    __tablename__ = 'services'
    __table_args__ = (
        Index('services_host_binary_idx', 'host', 'binary', unique=True),
    )

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    host = Column(String(255), nullable=False)
    binary = Column(String(255), nullable=False)
    topic = Column(String(255), nullable=False)
    rpc_version = Column(String(36), nullable=True)
    disabled = Column(Boolean, default=False)
    disabled_reason = Column(String(255), nullable=True)
    report_count = Column(Integer, default=0)
    availability_zone = Column(String(255), default='vigie')
    last_seen_up = Column(DateTime, nullable=True)

    def to_dict(self):
        return {
            'id': self.id,
            'host': self.host,
            'binary': self.binary,
            'topic': self.topic,
            'rpc_version': self.rpc_version,
            'disabled': self.disabled,
            'disabled_reason': self.disabled_reason,
            'report_count': self.report_count,
            'availability_zone': self.availability_zone,
            'last_seen_up': self.last_seen_up.isoformat() if self.last_seen_up else None,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
        }
