"""add_staff_shifts_tables

Revision ID: staff_shifts_001
Revises: guest_requests_001
Create Date: 2025-01-02 16:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'staff_shifts_001'
down_revision = 'guest_requests_001'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Create staff_shifts table
    op.create_table(
        'staff_shifts',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('staff_id', sa.Integer(), nullable=False),
        sa.Column('shift_date', sa.DateTime(), nullable=False),
        sa.Column('shift_type', sa.Enum('morning', 'afternoon', 'night', 'full_day', 'custom', name='shifttype'), nullable=False),
        sa.Column('start_time', sa.Time(), nullable=False),
        sa.Column('end_time', sa.Time(), nullable=False),
        sa.Column('status', sa.Enum('scheduled', 'in_progress', 'completed', 'cancelled', 'no_show', name='shiftstatus'), nullable=False, server_default='scheduled'),
        sa.Column('actual_start_time', sa.DateTime(), nullable=True),
        sa.Column('actual_end_time', sa.DateTime(), nullable=True),
        sa.Column('break_duration_minutes', sa.Integer(), nullable=True, server_default='30'),
        sa.Column('assigned_by', sa.Integer(), nullable=True),
        sa.Column('department', sa.String(length=100), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('handover_notes', sa.Text(), nullable=True),
        sa.Column('tasks_completed', sa.Integer(), nullable=True, server_default='0'),
        sa.Column('tasks_assigned', sa.Integer(), nullable=True, server_default='0'),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['assigned_by'], ['users.id'], ),
        sa.ForeignKeyConstraint(['staff_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_staff_shifts_staff_id'), 'staff_shifts', ['staff_id'], unique=False)
    op.create_index(op.f('ix_staff_shifts_shift_date'), 'staff_shifts', ['shift_date'], unique=False)

    # Create staff_tasks table
    op.create_table(
        'staff_tasks',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('shift_id', sa.Integer(), nullable=True),
        sa.Column('staff_id', sa.Integer(), nullable=False),
        sa.Column('title', sa.String(length=255), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('task_type', sa.String(length=100), nullable=False),
        sa.Column('priority', sa.Enum('low', 'normal', 'high', 'urgent', name='stafftaskpriority'), nullable=False, server_default='normal'),
        sa.Column('status', sa.Enum('pending', 'assigned', 'in_progress', 'completed', 'cancelled', 'on_hold', name='stafftaskstatus'), nullable=False, server_default='pending'),
        sa.Column('scheduled_start', sa.DateTime(), nullable=True),
        sa.Column('scheduled_end', sa.DateTime(), nullable=True),
        sa.Column('actual_start', sa.DateTime(), nullable=True),
        sa.Column('actual_end', sa.DateTime(), nullable=True),
        sa.Column('estimated_duration_minutes', sa.Integer(), nullable=True),
        sa.Column('actual_duration_minutes', sa.Integer(), nullable=True),
        sa.Column('assigned_by', sa.Integer(), nullable=True),
        sa.Column('due_date', sa.DateTime(), nullable=True),
        sa.Column('related_booking_id', sa.Integer(), nullable=True),
        sa.Column('related_room_id', sa.Integer(), nullable=True),
        sa.Column('related_guest_request_id', sa.Integer(), nullable=True),
        sa.Column('related_maintenance_id', sa.Integer(), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('completion_notes', sa.Text(), nullable=True),
        sa.Column('is_recurring', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('recurrence_pattern', sa.String(length=100), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['assigned_by'], ['users.id'], ),
        sa.ForeignKeyConstraint(['related_booking_id'], ['bookings.id'], ),
        sa.ForeignKeyConstraint(['related_guest_request_id'], ['guest_requests.id'], ),
        sa.ForeignKeyConstraint(['related_maintenance_id'], ['room_maintenance.id'], ),
        sa.ForeignKeyConstraint(['related_room_id'], ['rooms.id'], ),
        sa.ForeignKeyConstraint(['shift_id'], ['staff_shifts.id'], ),
        sa.ForeignKeyConstraint(['staff_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_staff_tasks_shift_id'), 'staff_tasks', ['shift_id'], unique=False)
    op.create_index(op.f('ix_staff_tasks_staff_id'), 'staff_tasks', ['staff_id'], unique=False)


def downgrade() -> None:
    op.drop_index(op.f('ix_staff_tasks_staff_id'), table_name='staff_tasks')
    op.drop_index(op.f('ix_staff_tasks_shift_id'), table_name='staff_tasks')
    op.drop_table('staff_tasks')
    op.drop_index(op.f('ix_staff_shifts_shift_date'), table_name='staff_shifts')
    op.drop_index(op.f('ix_staff_shifts_staff_id'), table_name='staff_shifts')
    op.drop_table('staff_shifts')

