# ProfitLeak AI — SaaS release candidate

PHP 8.4 / MySQL 8.4, server-rendered application, no Composer or JavaScript build required. This is a production-oriented starting point, not a certified or fully tested production service.

## Local startup
1. Install Docker Engine and Docker Compose v2.
2. Copy `.env.example` to `.env`. Replace both database secrets with separate random values (e.g. `openssl rand -hex 32`). Do not commit `.env`.
3. For local HTTP testing only, set `COOKIE_SECURE=0`.
4. Run `docker compose up -d --build` and open http://localhost:8080.
5. Create an account and use Run sample audit. A 14-day Starter trial starts immediately. The sample has 18 transactions, 3 customers, 2 products, 10,500 net revenue and 3,300 customer decline gap.
6. Run `docker compose exec app php tests/audit_test.php` (working directory is /var/www/html).

## Existing Apache / XAMPP
Use PHP 8.4+ with pdo_mysql and curl. Import db/schema.sql into a dedicated utf8mb4 MySQL database. Create a least-privileged DB user with SELECT/INSERT/UPDATE/DELETE on the application database. Set environment variables DB_DSN, DB_USER, DB_PASSWORD and COOKIE_SECURE via server configuration. Set the virtual host DocumentRoot to the project's `public` directory ONLY. Enable HTTPS, disable display_errors, set upload_max_filesize=10M and post_max_size=12M. Never expose the project root or database scripts. Use a dedicated virtual host rather than a shared public directory.

## Deployment
Docker binds to loopback port 8080. Put an HTTPS reverse proxy (e.g. Caddy or Nginx) in front, with a valid certificate, a request size cap of 12 MB, and login/upload rate limits. Set COOKIE_SECURE=1. A reverse proxy will cause the built-in IP throttle to share a bucket across users unless the application receives a securely resolved client address; configure trustworthy proxy handling or use proxy-level rate limiting before public launch. Never blindly trust forwarded IP headers. Configure encrypted database backups, restore testing, log rotation, uptime/health checks, resource limits, secret rotation, and patching. Docker app sessions are local to the container; restarting/replacing the container can invalidate sessions. Multi-instance deployments require a shared session store. Database initialization executes only on a fresh data volume; use explicit migrations for later versions. Do not run `docker compose down -v` on live data.

## Billing and AI
Manual invoicing only. Owner requests a subscription and an authorized server operator activates it after verified payment. Use `docker compose exec app php bin/subscription.php COMPANY_ID growth active`. Inspect company IDs from MySQL with authorized administrative access. Available plans: starter, growth, enterprise. Status: trial, active, suspended. There is no automated renewal or payment checkout; recurring invoice reconciliation and cancellation are manual. Prices are proposed, in USD, not validated. Limits are 20/200/2000 audit creations per UTC month and 3/15/100 active users respectively. Expired/suspended accounts retain read/export access. Optional AI is available for Growth/Enterprise when OPENAI_API_KEY is supplied. Only aggregated data is sent; obtain customer consent and confirm provider data handling before enabling. OPENAI_MODEL is configurable. Live API behavior has not been tested. Set BILLING_EMAIL for a contact link.

## CSV rules
UTF-8 comma-separated file, maximum 10 MB and 100,000 data rows. Required columns: date (YYYY-MM-DD), customer, product, revenue (signed decimal, at most two decimal places). One reporting currency per workspace, no FX conversion. Use anonymized identifiers where possible. Negative sales values are treated as returns. Latest calendar month present anchors the recent three months; prior three months form the baseline. All six months must appear in data; completeness cannot be verified. More than 20% decline versus positive prior revenue triggers a flag. The absolute decline is not a prediction or guaranteed recoverable revenue. Customer and product totals overlap. Raw files are not retained by the application; aggregate results and top 100 named customer/product decline records are retained. Export is JSON. Owners can delete audits. There is no automated retention or tenant deletion workflow yet.

## Release acceptance / outstanding gates
- Passed PHP syntax checks and deterministic audit tests (sample, returns, malformed input, short history).
- MySQL, Docker, browser and API integration could not be run in the build environment; run end-to-end testing before any real customer data.
- Test two separate companies: audit URLs/exports/deletes must never allow cross-tenant access. Test owner versus analyst, trial expiry, seat/audit quotas, logout, CSRF, invalid files, concurrent uploads, backups/restore, mobile layout and proxy limits.
- Add email verification, password recovery, secure invitations with mandatory password change, MFA, session revocation, abuse controls, privacy policy, terms, consent/retention workflows and legal/payment review before open self-service signup. Until then restrict access to invited pilot organizations via a proxy access gate. Disable or suspend accounts through operator process as needed.
- Automated subscriptions, checkout, webhooks, CRM/ERP connectors, full forecasting and automated email alerts are not implemented. Do not market the rule-based comparisons as a trained AI prediction model.
- No independent security audit or penetration testing completed. Do not deploy publicly with sensitive data until launch gates pass.
