#!/usr/bin/env python3

import os
import sys

import subprocess

from setuptools import setup, find_packages

# Steps

# 1.  Build the frontend assets
# Install node modules

print("Installing node modules...")

process = subprocess.run(
    "yarn install",
    shell=True,
    cwd=os.path.join(os.path.dirname(__file__), "frontend"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

if process.returncode > 0:
    print("Error installing node modules:")
    print(process.stderr.decode())
    sys.exit(1)

# Build the file assets

print("Building frontend assets...")

process = subprocess.run(
    "yarn build",
    shell=True,
    cwd=os.path.join(os.path.dirname(__file__), "frontend"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

if process.returncode > 0:
    print("Error building frontend assets:")
    print(process.stderr.decode())
    sys.exit(1)


print("Renaming and copying static files...")
# Rename the main css and js files to lexical.css and lexical.js
process = subprocess.run(
    '''
    js_file="$(find dist/assets -maxdepth 1 -type f -name 'index-*.js' | head -n1)"
[ -n "$js_file" ] && mv -v "$js_file" "dist/assets/lexical.js"
    ''',
    shell=True,
    cwd=os.path.join(os.path.dirname(__file__), "frontend"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

if process.returncode > 0:
    print("Error renaming js file:")
    print(process.stderr.decode())
    sys.exit(1)

process = subprocess.run(
    '''
    css_file="$(find dist/assets -maxdepth 1 -type f -name 'index-*.css' | head -n1)"
[ -n "$css_file" ] && mv -v "$css_file" "dist/assets/lexical.css"
    ''',
    shell=True,
    cwd=os.path.join(os.path.dirname(__file__), "frontend"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

if process.returncode > 0:
    print("Error renaming css file:")
    print(process.stderr.decode())
    sys.exit(1)


# Make directory lexical_editor/static/lexical_editor/assets if it doesn't exist
os.makedirs(
    os.path.join(
        os.path.dirname(__file__), "lexical_editor", "static", "lexical_editor", "assets"
    ),
    exist_ok=True,
)

# Now copy the frontend/dist/assets director to lexical_editor/static/lexical_editor/assets
process = subprocess.run(
    "cp -r dist/assets/* ../lexical_editor/static/lexical_editor/assets/",
    shell=True,
    cwd=os.path.join(os.path.dirname(__file__), "frontend"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

if process.returncode > 0:
    print("Error copying static files:")
    print(process.stderr.decode())
    sys.exit(1)

# 3.  Run setup

setup(
    name="django-lexical-editor",
    version="0.0.1",
    description="Django Lexical Editor integration.",
    author="Patrice Chaula",
    author_email="patrice@velocitymedia.co.za",
    url="https://github.com/velocity-digital/django-lexical-editor",
    project_urls={
        "Source": "https://github.com/velocity-digital/django-lexical-editor",
    },
    zip_safe=False,
    packages=find_packages(exclude=["frontend"]),
    install_requires=[
        "Django>=3.2",
        "django-js-asset>=2.0",
    ],
    python_requires=">=3.8",
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: 3.13",
        "License :: OSI Approved :: BSD License",
        "Operating System :: OS Independent",
        "Framework :: Django",
        "Framework :: Django :: 3.2",
        "Framework :: Django :: 4.1",
        "Framework :: Django :: 4.2",
        "Framework :: Django :: 5.0",
        "Framework :: Django :: 5.1",
        "Framework :: Django :: 5.2",
        "Intended Audience :: Developers",
        "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
    ],
)
