# SyncTide — Local Server Installation

This package is for the **local-machine server** deployment model.

In this model, one Windows machine inside the client site runs:
- PostgreSQL
- FastAPI backend
- Streamlit UI
- report generation
- scheduled jobs
- CSV ingestion

Users do **not** install the app on their own PCs. They access the UI from a browser over the local network.

## Expected project root

Place the runtime files inside one folder, for example:

```text
synctide/
  main.py
  Home.py
  ingest_csvs.py
  report_template_engine.py
  api_client.py
  paths.py
  platform_config.py
  settings.py
  init_db.py
  schema.sql
  register_templates.sql
  install_local.ps1
  install_local.bat
  start_backend.bat
  start_ui.bat
  start_ingestion_worker.bat
  start_local_server.bat
  requirements.txt
  env.example
  .streamlit/
    config.toml
  pages/
    1_Dashboard.py
    2_Reports.py
    3_SMS_Center.py
    4_User_Admin.py
    5_User_Configurations.py
    6_Configurations.py
    7_Equipment_Configuration.py
  report_templates/
    Report_template_1.xlsx
    Daily_Report_Template.xlsx
    Monthly_Report_Template.xlsx
    Yearly_Report_Template.xlsx
  raw_data/
  reports_output/
  logs/
```

## First installation

1. Copy the project folder to the local server machine.
2. Install PostgreSQL and create the `synctide` database.
3. Review `env.example` or `.env.example` and adjust database settings.
4. Run `install_local.bat`.
5. Review `.env` before first production use.

The installer will:
- create `.venv`
- install Python dependencies
- create runtime folders
- create `.streamlit/config.toml` from `config.toml` if needed
- create `.env` from `env.example` or `.env.example` if needed
- test the database connection
- apply the current canonical database bootstrap through `init_db.py`
- register the default report templates

## Starting the platform

### Backend
Run:

```bat
start_backend.bat
```

### UI
Run:

```bat
start_ui.bat
```

### Start both
Run:

```bat
start_local_server.bat
```

### Run ingestion manually
Run:

```bat
start_ingestion_worker.bat
```

## Access from other PCs

From another PC on the same LAN, open the UI in the browser using the local server IP, for example:

```text
http://192.168.1.50:8501
```

## Database bootstrap notes

- `schema.sql` is now the canonical bootstrap and is safe to re-run.
- `register_templates.sql` is safe to re-run and keeps the default templates aligned with the portable `report_templates/` paths.
- `init_db.py` applies both files using the database settings from `.env` through `settings.py`.

## Security recommendations for the local server

- Keep the project folder only on the server machine.
- Do not share the project folder over the network.
- Use a dedicated Windows user for running the platform.
- Limit RDP/admin access to trusted administrators only.
- Keep PostgreSQL accessible only locally if possible.
- Back up the database and `report_templates/` regularly.
- Back up `reports_output/` and `logs/` according to retention needs.
