"""
Sync service stubs — Claude Code should implement these following the pattern in copilot.py.
Each extends BaseSyncService and implements _execute_sync() + _generate_demo_data().
"""

from sqlalchemy.ext.asyncio import AsyncSession
from app.models.sync import DataSourceConfig
from app.services.sync.base import BaseSyncService, SyncResult


class PurviewAuditSyncService(BaseSyncService):
    """
    Purview Audit Log Sync — Pulls FileAccessed events from Microsoft Purview.

    Graph endpoint: POST /security/auditLog/queries
    Body: { displayName, filterStartDateTime, filterEndDateTime, recordTypeFilters: ["sharePointFileOperation"] }
    Then: GET /security/auditLog/queries/{id}/records

    Maps FileAccessed events to content_access_logs and creates completions for tracked SharePoint content.
    Also pulls CopilotInteraction events for detailed Copilot audit data.
    """
    source_name = "purview_audit"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        # 1. Create audit query job via Graph API
        # 2. Poll until complete
        # 3. Fetch records
        # 4. For FileAccessed events: match to tracked_links by sharepoint_item_id
        # 5. Create content_access_logs and update completions
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Generate demo file access events for tracked content
        return SyncResult()


class VivaLearningSyncService(BaseSyncService):
    """
    Viva Learning Sync — Pulls learning completions from Microsoft Viva.

    Graph endpoint: GET /employeeExperience/learningProviders/{id}/learningCourseActivities
    Maps Viva course completions to activity_completions.
    """
    source_name = "viva_learning"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Generate demo Viva completions
        return SyncResult()


class UdemySyncService(BaseSyncService):
    """
    Udemy Business Sync — Pulls course catalog and user completions.

    REST API: GET /api-2.0/organizations/{account_id}/courses/list/
    REST API: GET /api-2.0/organizations/{account_id}/analytics/user-course-activity/
    Maps completions by matching user email to DWA employees.
    """
    source_name = "udemy"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        # 1. Fetch course catalog, upsert into activities table
        # 2. Fetch user-course-activity, match by email
        # 3. Create/update completions
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Generate demo Udemy completions
        return SyncResult()


class LinkedInLearningSyncService(BaseSyncService):
    """
    LinkedIn Learning Sync — Pulls learner activity and completions.

    REST API: GET /v2/learningActivityReports
    Maps by matching LinkedIn email to DWA employee email.
    """
    source_name = "linkedin_learning"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Generate demo LinkedIn completions
        return SyncResult()


class GitHubCopilotSyncService(BaseSyncService):
    """
    GitHub Copilot Metrics Sync — Pulls org-level Copilot usage metrics.

    REST API: GET /orgs/{org}/copilot/metrics
    REST API: GET /orgs/{org}/copilot/billing/seats (for per-user seat data)

    Maps by matching github_username on User model.
    Creates monthly tool_usage completions for active users.
    """
    source_name = "github_copilot"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Generate demo GitHub Copilot usage
        return SyncResult()


class ExcelImportSyncService(BaseSyncService):
    """
    Excel/CSV Import Handler — Processes GenSpark and Claude usage exports.

    Expected CSV format for GenSpark:
      email, session_count, last_active, total_queries

    Expected CSV format for Claude:
      email, conversation_count, tokens_used, last_active

    Admin uploads file via /api/import/excel, this service processes it.
    """
    source_name = "excel_import"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # This is triggered by file upload, not scheduled
        # The file path would be in config.config["file_path"]
        # TODO: Implement file parsing and completion creation
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # Demo data for GenSpark/Claude is created in seed script
        return SyncResult()


class SharePointAnalyticsSyncService(BaseSyncService):
    """
    SharePoint Analytics Sync — Pulls file view analytics from SharePoint.

    Graph endpoint: GET /sites/{site-id}/analytics/allTime
    Graph endpoint: GET /drives/{drive-id}/items/{item-id}/analytics/allTime?$expand=activities($filter=access ne null)

    Requires SharePoint Viewers feature to be activated on target sites.
    Cross-references with tracked_links by sharepoint_item_id.
    """
    source_name = "sharepoint_analytics"

    async def _execute_sync(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        # TODO: Implement
        return SyncResult()

    async def _generate_demo_data(self, db: AsyncSession, config: DataSourceConfig) -> SyncResult:
        return SyncResult()
