"""Media database service for database operations."""

import logging
from typing import Any, Dict, List, Optional
from uuid import UUID

from ..repositories.media_repository import MediaRepositoryProtocol, create_media_repository
from ..database.models import MediaFile
from .media_types import MediaFileInfo, MediaStatus

logger = logging.getLogger(__name__)


class MediaDatabaseService:
    """Service for media database operations."""

    def __init__(self, media_repository: Optional[MediaRepositoryProtocol] = None):
        self.media_repository = media_repository or create_media_repository()

    async def create_media_file_record(self, media_info: MediaFileInfo, youtube_video_id: Optional[UUID] = None) -> MediaFile:
        """Create a media file record in the database."""
        media_data = {
            "filename": media_info.filename,
            "file_size": media_info.file_size,
            "duration": media_info.duration,
            "mime_type": media_info.mime_type,
            "source_path": media_info.source_path,
            "local_path": media_info.local_path,
            "file_hash": media_info.file_hash,
            "file_metadata": media_info.metadata,
            "status": MediaStatus.PENDING.value,
        }

        if youtube_video_id:
            media_data["youtube_video_id"] = youtube_video_id

        return await self.media_repository.create(media_data)

    async def update_media_file_status(self, media_id: UUID, status: str) -> Optional[MediaFile]:
        """Update media file status in the database."""
        return await self.media_repository.update_status(media_id, status)

    async def get_media_file_by_id(self, media_id: UUID) -> Optional[MediaFile]:
        """Get media file by ID from the database."""
        return await self.media_repository.get_by_id(media_id)

    async def get_pending_media_files(self, limit: int = 50) -> List[MediaFile]:
        """Get pending media files from the database."""
        return await self.media_repository.get_pending_files(limit)

    async def get_ready_media_files(self, limit: int = 50) -> List[MediaFile]:
        """Get ready media files from the database."""
        return await self.media_repository.get_ready_files(limit)

    async def get_media_files_by_status(self, status: str, limit: int = 50) -> List[MediaFile]:
        """Get media files by status from the database."""
        return await self.media_repository.get_by_status(status, limit)

    async def update_media_file(self, media_id: UUID, media_data: Dict[str, Any]) -> Optional[MediaFile]:
        """Update media file record in the database."""
        return await self.media_repository.update(media_id, media_data)

    async def delete_media_file(self, media_id: UUID) -> bool:
        """Delete media file record from the database."""
        return await self.media_repository.delete(media_id)

    async def get_media_file_by_filename(self, filename: str) -> Optional[MediaFile]:
        """Get media file by filename from the database."""
        return await self.media_repository.get_by_filename(filename)

    async def get_media_file_by_hash(self, file_hash: str) -> Optional[MediaFile]:
        """Get media file by file hash from the database."""
        return await self.media_repository.get_by_file_hash(file_hash)

    async def list_all_media_files(self, limit: int = 100, offset: int = 0) -> List[MediaFile]:
        """List all media files with pagination."""
        return await self.media_repository.list_all(limit, offset)

    async def get_media_files_by_youtube_video_id(self, youtube_video_id: UUID, limit: int = 50) -> List[MediaFile]:
        """Get media files by YouTube video ID from the database."""
        return await self.media_repository.get_by_youtube_video_id(youtube_video_id, limit)
