# TGT voice/SMS corrections

## Scope and safety

This change is in the common Symfony backend only. It does not call tgt-gateway,
change payment routing, buy eSIMs, modify prices, release profiles, change expiry
logic, or add a cron. Existing Maya/Telnyx feature presentation is preserved.
Feature details appear in the purchase email body, not the invoice PDF.

The working tree also contains unfinished payment work. Review the diff and ship
the feature correction separately; do not treat that unrelated work as approved
for production by this document.

## Correct interpretation

- Mongolia combined "Unlimited nationwide calls and SMS" exposes SMS support.
- Korea incoming calls are not advertised as unlimited local outgoing calls.
  The additional outgoing bundle requires top-up, and 5 outgoing minutes require KYC.
- Free SMS reception is incoming support, not an unlimited outgoing SMS bundle.
- Same-network local minutes and zone-specific benefits retain their restrictions.
- Conditional KYC is not translated into `kyc_required=false`.
- A balance/activation SMS command is not an SMS entitlement.
- Unrecognized claims remain unknown and carry per-feature review flags.
  `recognized` means at least one explicit fact was found, not that every feature
  is verified. Check `service_details.review` for incomplete facts.
- Provider text remains the evidence, not a guarantee of service. New wording
  needs review and regression tests; this parser does not infer arbitrary prose.

Old response keys remain. Where a simple old field would overstate the benefit,
it is NULL. Additive `service_details` contains voice/SMS arrays with direction,
scope, count, unlimited, included, and condition, plus KYC and review details.
`count` means minutes in voice arrays and messages in SMS arrays. Unknown
quantities are NULL. `has_voice`/`voice_supported` means service support, not that
all outgoing calls are included. Client apps should use details/terms for labels,
not label every `voice_supported=true` plan as free outgoing calls.

Raw evidence, parser version, source fields and source hash are internal only.
TGT/TUGE branding and source URLs are redacted from feature presentation.
Existing QR, SM-DP+ and installation values are unchanged.

Manual/manual_override fields are not re-parsed or repaired. Automatically
generated structured details are not used to supersede manual legacy fields.
Older automatic subscription snapshots are reinterpreted for display/email only;
their stored purchase rows are not rewritten. If a legacy snapshot has no terms,
features remain unknown rather than copying today's catalogue into an old sale.

## Schema first

No new table. Add nullable `service_details JSON` to:

- `esim_plan_subscriptions`
- `user_esim_subscriptions`

Existing feature columns remain. The JSON stores structured restrictions,
evidence and parser provenance; subscriptions snapshot these details on purchase.

1. Back up and test on staging first. Confirm the target database explicitly.
2. Apply `src/AppBundle/Resources/sql/add_esim_service_details.sql` to **every
   database served by this shared deployment** before deploying entity changes.
   Doctrine reads the new fields even for other apps/providers.
3. The SQL requests online DDL with a short metadata-lock wait. If unsupported or
   blocked, stop and schedule the migration; do not remove the lock protections
   on a busy production server. Check SHOW COLUMNS after partial failure and run
   only the missing ALTER. Never use automatic schema:update --force here.
4. Deploy the reviewed code, then clear/warm cache as the web service account:

```sh
sudo -u www-data php bin/console cache:clear --env=prod --no-debug
sudo -u www-data php bin/console cache:warmup --env=prod --no-debug
sudo -u www-data php bin/console app:reconcile-tgt-features --help --env=prod --no-debug
```

Check plan listing, existing eSIM listing and email rendering in staging before
production rollout. Existing cron schedules remain unchanged.

## Review and repair catalogue features

`--env=prod` selects Symfony configuration. `--provider-environment=prod` selects
the stored provider catalogue, NOT the database server. Verify parameters.yml on
the server you are using. Reports must be private, outside web/, and never committed.

Preview Mongolia first (no database writes and no provider HTTP calls):

```sh
sudo -u www-data php bin/console app:reconcile-tgt-features \
  --provider-environment=prod \
  --product-code='A-033-ES-AU-T-au1-7D/60D-8GB' \
  --report=/tmp/tgt-mongolia-review.json --env=prod --no-debug
```

Review `before`, `after`, and the decoded `service_details` in the report. Only
after approval apply that exact report:

```sh
sudo -u www-data php bin/console app:reconcile-tgt-features \
  --provider-environment=prod --apply-report=/tmp/tgt-mongolia-review.json \
  --confirm --env=prod --no-debug
```

Then preview the complete catalogue using a NEW report filename:

```sh
sudo -u www-data php bin/console app:reconcile-tgt-features \
  --provider-environment=prod --report=/tmp/tgt-all-features-review.json \
  --env=prod --no-debug
```

Review Korea/top-up claims, zone minutes, same-network scope, conditional KYC and
all review flags before applying the full report with the same apply/confirm
syntax. Existing report files are never overwritten.

Application locks each plan, checks provider/environment, rejects stale source
or feature edits, recomputes proposed values, and updates only the feature-column
allowlist. A report from another database/parser version cannot be applied.
Exit 2 means stale/ineligible rows were skipped: generate a new report, do not
force overwrite them. Each plan commits separately; if interrupted, preview again.
Replay cannot provision or email anything. Keep the report as the before-image
for any separately reviewed rollback; no automatic historical subscription repair.

Legacy catalogue repair uses only the original stored `network_operator` text;
unavailable provider fields are not invented. Normal existing TGT sync parses
the full current product (operatorDesc, ruleDesc, showGradeContent, productName)
and refreshes provenance on every sync, preserving manual overrides. Do not use
full price/package sync just to repair features.

## Verification and rollback

Local tests cover audited descriptions, old snapshot containment, actual API
formatting, sync/snapshot setters, email rendering, manual overrides, report-only
defaults, reviewed apply, stale reports, JSON normalization and wrong-database rejection.
The connected catalogue was queried read-only; no schema, sync, purchase, email
or repair was executed against it during implementation. Staging/production DDL
and real purchase smoke testing are still deployment tasks.
Tests ran on PHP 8.1; changed PHP files also pass PHP 7.4 syntax checks. The local
PHPUnit runner's dependencies require 8.1, so a complete PHP 7.4 runtime test was
not possible with that runner. Validate the actual production PHP/runtime in staging.

If rollback is needed, pause catalogue sync first to prevent the old parser from
restoring misleading flags. Keep nullable JSON columns in place; old code ignores
them. Review report before-images before any data rollback. Never restore incorrect
"unlimited local calls" labels just to match the old parser.
