# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Commands

### PHP / Laravel
```bash
php artisan serve                          # Dev server (port 8000)
php artisan migrate                        # Run pending migrations
php artisan migrate:fresh --seed           # Full reset + seed (dev only)
php artisan db:seed --class=InitialSetupSeeder
php artisan queue:work                     # Process jobs (required for async payments)
php artisan schedule:run                   # Trigger scheduled tasks manually

# Business commands
php artisan credits:reconcile-status       # Dry-run: preview status changes
php artisan credits:reconcile-status --apply  # Apply status reconciliation
php artisan credits:update-installments    # Recompute installment due dates
php artisan subscriptions:update-status    # Update SaaS subscription statuses
```

### Testing
```bash
php artisan test                           # All tests
php artisan test --testsuite=Unit          # Unit suite only
php artisan test --testsuite=Feature       # Feature suite only
php artisan test tests/Unit/SmartCreditStateResolverTest.php  # Single file
php artisan test --filter=test_method_name # Single test by name
```

Tests use a real MariaDB database (`credify` / `credifyuser` / `credifypass`). `QUEUE_CONNECTION=sync` in test env — jobs run inline.

### Static Analysis & Linting
```bash
./vendor/bin/phpstan analyse              # Level 5, paths: app/
./vendor/bin/pint                         # Laravel Pint (code style)
./vendor/bin/pint --test                  # Dry-run check only
```

### Frontend / PWA
```bash
npm run dev                               # Vite dev server (watches)
npm run build                             # Production build (Filament + PWA)
npm run build:pwa                         # Alias for build
```

The PWA Service Worker source is `resources/js/pwa/pwa-sw.js` → compiles to `public/pwa-sw.js`. After any SW change, run `npm run build`.

---

## Architecture Overview

### Multi-Tenancy
Every major Eloquent model uses the `MultiTenantScope` trait which automatically:
- Sets `company_id` on `creating` from `Auth::user()->company_id`
- Adds a global scope filtering all queries by `company_id`
- Skips both when running in console or unauthenticated
- `super_admin` role bypasses the filter and sees all companies

### Roles (Spatie Permissions)
Five roles: `super_admin`, `admin`, `supervisor`, `collector`, `client`.
PWA access requires `admin`, `supervisor`, or `collector` (`pwa.access` middleware).
Some routes additionally require `collector` only (`pwa.collector` middleware).
Role detection in PWA controllers uses the `RoleAwareQueries` trait — priority order: admin > supervisor > collector.

### Credit Lifecycle
- Credits can be parent/child (extend, refinance, restructure, renew create child credits).
- **Only leaf credits** (no children) are operational: they appear in dashboards, KPIs, routes, and aging reports.
- Parent credits never change status automatically.
- Active statuses: `active`, `delayed`, `overdue`. Immutable terminal statuses: `canceled`, `extended`, `refinanced`, `restructured`, `renewed`, `interest_capitalized`, `defaulted`, `archived`.
- `SmartCreditStateResolver` → pure state calculation (no side effects), use `resolveMany()` for batch to avoid N+1.
- `CreditStatusSyncService` → persists state with deduplication per request; `forceSync()` for jobs/reconciliation.
- `SyncCreditStatusJob` → async job (3 tries, backoff 60/300/900s, `WithoutOverlapping`), dispatched by `RefreshCreditStatusListener` on `PaymentRegistered`/`PaymentReversed`.
- Nightly reconciliation: `credits:reconcile-status --apply` at 00:10 via scheduler.

### Payment System
- `payment_installment` pivot table is the source of truth (`applied_amount` per installment).
- `amount_paid` on installments is materialized from the pivot by `PaymentMaterializer`.
- `PaymentManager` orchestrates: Registrar → Distributor → Materializer → InvariantGuard.
- `PaymentInvariantGuard` throws `RuntimeException` on post-payment inconsistency.
- Idempotency key (UUID) on `payments` table prevents offline duplicates.
- Reversals create a compensatory negative `Payment` — originals are never deleted.

### Money / Arithmetic
- All monetary values use `Money` value object (`app/ValueObjects/Money.php`) backed by BC Math.
- **No PHP floats** anywhere. DB columns are `DECIMAL(12,2)`. Internal precision: 8 decimals.
- Last installment absorbs rounding residues.
- Dates for payments always use `Carbon::today()`, never `now()`.

### Finance Auto-Logger
`FinanceAutoLogger` is the single gateway for creating `Income` and `Expense` records. Never create them directly via Eloquent.

### Scheduled Commands (bootstrap/app.php)
| Time | Command |
|------|---------|
| 00:00 | `subscriptions:update-status` |
| 00:05 | `credits:update-installments` |
| 00:10 | `credits:reconcile-status --apply` |

### Filament Admin Panel
- `app/Filament/Resources/` — one resource per domain entity.
- Credit operations (extend, refinance, restructure, renew, settle) are implemented as Filament Actions inside `Credits/Actions/`.
- `CreditOperationOrchestrator` + `CreditOperationValidator` handle the shared logic for all credit transformation operations.
- Interest strategies in `app/Services/Interest/`: `FlatRateStrategy`, `DiminishingBalanceStrategy`, `FlatRateProjectedStrategy`.

### PWA (Vue 3 Offline-First)
- Entry: `resources/js/pwa/main.js` → Vue Router + Pinia.
- IndexedDB via Dexie (`resources/js/pwa/db/index.js`): stores `clients`, `credits`, `installments`, `payments`, `pendingPayments`, `pendingVisits`.
- Pinia stores in `resources/js/pwa/stores/`: `auth`, `sync`, `team`, `collections`, `dashboard`, `approvals`, `delinquency`, `financials`, `partners`, `visits`, `settings`.
- Offline auth: `isAuthenticated` accepts a cached user when `!navigator.onLine`.
- All dates use `getLocalDateString()` from `resources/js/pwa/utils/` — never `toISOString()` (UTC offset bug).
- Sync strategy: `syncInitialData()` loads 7 days of data on login; pending payments/visits sync in batch when back online.
- SW uses `injectManifest` strategy — precaches all `build/assets/**/*.{js,css}` + PWA icons.
- Alias `@pwa` → `resources/js/pwa/`.

### PWA API Routes (`routes/api.php`)
- Public: `POST /api/pwa/auth/login` (rate-limited `pwa-login`).
- Protected: `auth:sanctum` + `pwa.access` — all other PWA endpoints.
- Collector-only: `auth:sanctum` + `pwa.collector` — payment registration, visit registration.
- Token stored in memory (Pinia), not localStorage. No httpOnly cookie migration yet.

### Supervisor–Collector Assignment
`SupervisorCollector` model + `CollectorVisibilityResolver` service control which collectors a supervisor can see. `sees_all_collectors` flag on users bypasses assignment filtering.

### SaaS Layer
`Plan` → `Subscription` (states: trial → active → grace → expired) → `SubscriptionRequest`. Managed via Filament resources under `app/Filament/Resources/Subscriptions/` and `SubscriptionRequests/`.

### Collector Route Order
`CollectorCreditOrder` model + `CollectorCreditOrderService`: `reindexCollector()` uses `ROW_NUMBER() OVER` with `lockForUpdate` to be thread-safe. `CreditObserver::addOrderRowAtBottom()` is the only safe entry point for inserting new order rows.

### Key Supporting Files
- `app/ValueObjects/Money.php` — monetary value object
- `app/Support/MoneyCalculator.php` — arithmetic helpers
- `app/Traits/MultiTenantScope.php` — global tenant isolation
- `app/Services/SmartCreditStateResolver.php` — pure credit state logic
- `app/Services/CreditStatusSyncService.php` — state persistence
- `app/Services/Payments/PaymentManager.php` — payment orchestration
- `app/Services/FinanceAutoLogger.php` — income/expense gateway
- `app/Observers/CreditObserver.php` — collector order maintenance
- `resources/js/pwa/db/index.js` — Dexie schema definition
- `resources/js/pwa/stores/sync.js` — offline queue logic

===

<laravel-boost-guidelines>
=== foundation rules ===

# Laravel Boost Guidelines

The Laravel Boost guidelines are specifically curated by Laravel maintainers for this application. These guidelines should be followed closely to ensure the best experience when building Laravel applications.

## Foundational Context

This application is a Laravel application and its main Laravel ecosystems package & versions are below. You are an expert with them all. Ensure you abide by these specific packages & versions.

- php - 8.3
- filament/filament (FILAMENT) - v4
- laravel/framework (LARAVEL) - v12
- laravel/prompts (PROMPTS) - v0
- laravel/sanctum (SANCTUM) - v4
- livewire/livewire (LIVEWIRE) - v3
- larastan/larastan (LARASTAN) - v3
- laravel/boost (BOOST) - v2
- laravel/mcp (MCP) - v0
- laravel/pail (PAIL) - v1
- laravel/pint (PINT) - v1
- laravel/sail (SAIL) - v1
- phpunit/phpunit (PHPUNIT) - v11
- vue (VUE) - v3
- tailwindcss (TAILWINDCSS) - v3

## Skills Activation

This project has domain-specific skills available. You MUST activate the relevant skill whenever you work in that domain—don't wait until you're stuck.

- `laravel-best-practices` — Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strategies, authorization and security patterns, validation, error handling, queue and job configuration, route definitions, and architectural decisions. Also use for Laravel code reviews and refactoring existing Laravel code to follow best practices. Covers any task involving Laravel backend PHP code patterns.
- `tailwindcss-development` — Always invoke when the user's message includes 'tailwind' in any form. Also invoke for: building responsive grid layouts (multi-column card grids, product grids), flex/grid page structures (dashboards with sidebars, fixed topbars, mobile-toggle navs), styling UI components (cards, tables, navbars, pricing sections, forms, inputs, badges), adding dark mode variants, fixing spacing or typography, and Tailwind v3/v4 work. The core use case: writing or fixing Tailwind utility classes in HTML templates (Blade, JSX, Vue). Skip for backend PHP logic, database queries, API routes, JavaScript with no HTML/CSS component, CSS file audits, build tool configuration, and vanilla CSS.

## Conventions

- You must follow all existing code conventions used in this application. When creating or editing a file, check sibling files for the correct structure, approach, and naming.
- Use descriptive names for variables and methods. For example, `isRegisteredForDiscounts`, not `discount()`.
- Check for existing components to reuse before writing a new one.

## Verification Scripts

- Do not create verification scripts or tinker when tests cover that functionality and prove they work. Unit and feature tests are more important.

## Application Structure & Architecture

- Stick to existing directory structure; don't create new base folders without approval.
- Do not change the application's dependencies without approval.

## Frontend Bundling

- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them.

## Documentation Files

- You must only create documentation files if explicitly requested by the user.

## Replies

- Be concise in your explanations - focus on what's important rather than explaining obvious details.

=== boost rules ===

# Laravel Boost

## Tools

- Laravel Boost is an MCP server with tools designed specifically for this application. Prefer Boost tools over manual alternatives like shell commands or file reads.
- Use `database-query` to run read-only queries against the database instead of writing raw SQL in tinker.
- Use `database-schema` to inspect table structure before writing migrations or models.
- Use `get-absolute-url` to resolve the correct scheme, domain, and port for project URLs. Always use this before sharing a URL with the user.
- Use `browser-logs` to read browser logs, errors, and exceptions. Only recent logs are useful, ignore old entries.

## Searching Documentation (IMPORTANT)

- Always use `search-docs` before making code changes. Do not skip this step. It returns version-specific docs based on installed packages automatically.
- Pass a `packages` array to scope results when you know which packages are relevant.
- Use multiple broad, topic-based queries: `['rate limiting', 'routing rate limiting', 'routing']`. Expect the most relevant results first.
- Do not add package names to queries because package info is already shared. Use `test resource table`, not `filament 4 test resource table`.

### Search Syntax

1. Use words for auto-stemmed AND logic: `rate limit` matches both "rate" AND "limit".
2. Use `"quoted phrases"` for exact position matching: `"infinite scroll"` requires adjacent words in order.
3. Combine words and phrases for mixed queries: `middleware "rate limit"`.
4. Use multiple queries for OR logic: `queries=["authentication", "middleware"]`.

## Artisan

- Run Artisan commands directly via the command line (e.g., `php artisan route:list`). Use `php artisan list` to discover available commands and `php artisan [command] --help` to check parameters.
- Inspect routes with `php artisan route:list`. Filter with: `--method=GET`, `--name=users`, `--path=api`, `--except-vendor`, `--only-vendor`.
- Read configuration values using dot notation: `php artisan config:show app.name`, `php artisan config:show database.default`. Or read config files directly from the `config/` directory.
- To check environment variables, read the `.env` file directly.

## Tinker

- Execute PHP in app context for debugging and testing code. Do not create models without user approval, prefer tests with factories instead. Prefer existing Artisan commands over custom tinker code.
- Always use single quotes to prevent shell expansion: `php artisan tinker --execute 'Your::code();'`
  - Double quotes for PHP strings inside: `php artisan tinker --execute 'User::where("active", true)->count();'`

=== php rules ===

# PHP

- Always use curly braces for control structures, even for single-line bodies.
- Use PHP 8 constructor property promotion: `public function __construct(public GitHub $github) { }`. Do not leave empty zero-parameter `__construct()` methods unless the constructor is private.
- Use explicit return type declarations and type hints for all method parameters: `function isAccessible(User $user, ?string $path = null): bool`
- Use TitleCase for Enum keys: `FavoritePerson`, `BestLake`, `Monthly`.
- Prefer PHPDoc blocks over inline comments. Only add inline comments for exceptionally complex logic.
- Use array shape type definitions in PHPDoc blocks.

=== deployments rules ===

# Deployment

- Laravel can be deployed using [Laravel Cloud](https://cloud.laravel.com/), which is the fastest way to deploy and scale production Laravel applications.

=== tests rules ===

# Test Enforcement

- Every change must be programmatically tested. Write a new test or update an existing test, then run the affected tests to make sure they pass.
- Run the minimum number of tests needed to ensure code quality and speed. Use `php artisan test --compact` with a specific filename or filter.

=== laravel/core rules ===

# Do Things the Laravel Way

- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.). You can list available Artisan commands using `php artisan list` and check their parameters with `php artisan [command] --help`.
- If you're creating a generic PHP class, use `php artisan make:class`.
- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior.

### Model Creation

- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things, using `php artisan make:model --help` to check the available options.

## APIs & Eloquent Resources

- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention.

## URL Generation

- When generating links to other pages, prefer named routes and the `route()` function.

## Testing

- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model.
- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`.
- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests.

## Vite Error

- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`.

=== laravel/v12 rules ===

# Laravel 12

- CRITICAL: ALWAYS use `search-docs` tool for version-specific Laravel documentation and updated code examples.
- Since Laravel 11, Laravel has a new streamlined file structure which this project uses.

## Laravel 12 Structure

- In Laravel 12, middleware are no longer registered in `app/Http/Kernel.php`.
- Middleware are configured declaratively in `bootstrap/app.php` using `Application::configure()->withMiddleware()`.
- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files.
- `bootstrap/providers.php` contains application specific service providers.
- The `app/Console/Kernel.php` file no longer exists; use `bootstrap/app.php` or `routes/console.php` for console configuration.
- Console commands in `app/Console/Commands/` are automatically available and do not require manual registration.

## Database

- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost.
- Laravel 12 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`.

### Models

- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models.

=== pint/core rules ===

# Laravel Pint Code Formatter

- If you have modified any PHP files, you must run `vendor/bin/pint --dirty --format agent` before finalizing changes to ensure your code matches the project's expected style.
- Do not run `vendor/bin/pint --test --format agent`, simply run `vendor/bin/pint --format agent` to fix any formatting issues.

=== phpunit/core rules ===

# PHPUnit

- This application uses PHPUnit for testing. All tests must be written as PHPUnit classes. Use `php artisan make:test --phpunit {name}` to create a new test.
- If you see a test using "Pest", convert it to PHPUnit.
- Every time a test has been updated, run that singular test.
- When the tests relating to your feature are passing, ask the user if they would like to also run the entire test suite to make sure everything is still passing.
- Tests should cover all happy paths, failure paths, and edge cases.
- You must not remove any tests or test files from the tests directory without approval. These are not temporary or helper files; these are core to the application.

## Running Tests

- Run the minimal number of tests, using an appropriate filter, before finalizing.
- To run all tests: `php artisan test --compact`.
- To run all tests in a file: `php artisan test --compact tests/Feature/ExampleTest.php`.
- To filter on a particular test name: `php artisan test --compact --filter=testName` (recommended after making a change to a related file).

</laravel-boost-guidelines>
