from __future__ import annotations

import datetime
import os
import uuid
from concurrent.futures import ThreadPoolExecutor
from time import ctime

import file_widgets
from request_client import RequestClient
from sqlmodel import Field, Session, SQLModel, create_engine, select


class FolderStructure(file_widgets.Folder):
    files: list[file_widgets.File]
    folders: list[FolderStructure]

    def find_folder(self, name: str) -> file_widgets.File | None:
        for file in self.folders:
            if file.folder_name == name:
                return file

    def find_file(self, name: str) -> file_widgets.File | None:
        for file in self.files:
            if file.file_name == name:
                return file

    @staticmethod
    def get_structure(folder_id: uuid.UUID) -> FolderStructure:
        return FolderStructure.model_validate_json(
            RequestClient()
            .client.get("/folders/structure", params={"folder_id": folder_id})
            .text
        )


class SyncData(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    path: str
    last_updated: datetime.datetime
    folder_id: uuid.UUID

    @staticmethod
    def sync_all():
        with Session(engine) as s:
            syncs = s.exec(select(SyncData)).fetchall()
        with ThreadPoolExecutor(3) as pool:
            map_ = pool.map(
                lambda sync: merge_folder(
                    sync.path,
                    FolderStructure.get_structure(sync.folder_id),
                    sync.last_updated,
                ),
                syncs,
            )
            tuple(map_)

    @staticmethod
    def new_sync(path: str, parent_id):
        name = os.path.basename(path)
        folder_id = file_widgets.Folder.create(name, parent_id)
        with Session(engine) as s:
            sync = SyncData(
                path=path,
                last_updated=datetime.datetime.now(),
                folder_id=folder_id,
            )
            s.add(sync)
            s.commit()
        upload_folder(path, folder_id)


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)


def upload_folder(path: str, folder_id: uuid.UUID):
    items = os.listdir(path)
    for item in items:
        item_path = os.path.join(path, item)
        if os.path.isfile(item_path):
            file_widgets.File.create(item_path, folder_id)
        elif os.path.isdir(item_path):
            id = file_widgets.Folder.create(item, folder_id)
            upload_folder(item_path, id)


def merge_folder(
    path: str,
    structure: FolderStructure,
    time: datetime.datetime,
):
    for item in os.listdir(path):
        item_path = os.path.join(path, item)
        if os.path.isfile(item_path):
            file = structure.find_file(item)
            if file is None:
                file_widgets.File.create(item_path, structure.folder_id)
                continue
            mtime = datetime.datetime.strptime(
                ctime(os.path.getmtime(item_path)), "%a %b %d %H:%M:%S %Y"
            )
            print(item, mtime, time)
            if mtime > time:
                file.modify(item_path)
            else:
                file.download(item_path)
        elif os.path.isdir(item_path):
            folder = structure.find_folder(item)
            if folder is None:
                folder_id = file_widgets.Folder.create(item, structure.folder_id)
                upload_folder(item_path, folder_id)
            else:
                merge_folder(item_path, folder, time)
