import os
import sys
from pathlib import Path
import shutil

from apps import SubApp
### fastorg setup basic app multiverse include docker

class CreateInit:
    def __init__(self, docker_file="docker-compose.yml"):

        self.docker_file : str = docker_file
        self.current_path : str = Path.cwd()
        self.target_path : str  = self.current_path / '__init__.py'


    def create(self) -> bool:
        if self.target_path.exists():
            return True
        
        self.target_path.touch()
        print(f"File {self.target_path} created successfully.")

        return True
    
    def create_docker(self) -> bool:
        docker_path = self.current_path / self.docker_file
        if docker_path.exists():
            return True
        
        ##fill the docker
        docker_path.touch()
        print(f"File {docker_path} created successfully.")
        return True
    
class ConfigSetup:
    def __init__(self, config_file : dict) -> bool:

        self.config_file = config_file
        self.current_path = Path.cwd()
        self.target_path = self.current_path / self.config_file['mainfile']

    def create(self) -> bool:
        if self.target_path.exists():
            return True
        
        source = Path(__file__).parent / 'config.py'
        target = self.target_path / 'config.py'

        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy(source, target)
        print(f"File {target} created successfully.")

        return True
        

#apps  dependency
class MainCreate:
    def __init__(self, mainfile, subapps : list[SubApp] = []):
        self.mainfile = mainfile
        self.current_path = Path.cwd()
        self.target_path = self.current_path / self.mainfile
        self.subapps = subapps

        self.memory = ''

    def create_releation_pattern(self, subappname):
        self.memory += f'''from .{subappname}.router import router as {subappname}_router\n'''

    def create_main_pattern(self, subappname):
        self.memory += f'''app.include_router({subappname}_router)\n'''

    def create_subapps(self):
        self.memory += f'''from .config import app\n'''
        for subapp in self.subapps:
            self.create_releation_pattern(subapp.name)
            self.create_main_pattern(subapp.name)
        
    def create(self):
        if self.target_path.exists():
            return True
        
        print("target path -----------------------------> ", self.target_path)
        self.create_subapps()

        with open(self.target_path, 'a') as f:
            f.write(self.memory.strip())
        
        print(f"File {self.target_path} created successfully.")

        return True
    

### -init -docker -config
class BasicSetup:
    def __init__(self, mainfile, docker_file, db_type, db_name):
        self.subapps = []
        self.mainfile = mainfile
        self.docker_file = docker_file
        self.config_file_setuping = {
            'mainfile': "config.py",
            'db_type': db_type,
            'db_name': db_name,
            'db_user' : 'user',
            'db_password' : 'password',
            'db_host' : 'localhost',
            'db_port' : 5432
        }
        self.hasInit = False
        self.hasDocker = False
        self.hasConfig = False



    def proceed(self):
        self.create_init = CreateInit(self.docker_file)
        self.config_setup = ConfigSetup(self.config_file_setuping)
        self.main_create = MainCreate(self.mainfile, self.subapps)


    def create(self):

        self.proceed()

        try:
            pass
            self.hasInit = self.create_init.create()
            self.hasDocker = self.create_init.create_docker()
            self.hasConfig = self.config_setup.create()
            self.main_create.create()

        except Exception as e:
            print(f"Error creating setup: {e}")
            return
        
