Technical Specification – Medical Micro SaaS (Extended & Final) 1. System Overview The system is a multi-tenant medical Micro SaaS, built with Laravel, designed to serve: Independent doctors Medical clinics with multiple doctors and staff The platform must provide: Strong data isolation per tenant Subscription-based access Medical-grade auditability Integrated AI features Native billing via Mercado Pago API 2. Actors and Roles 2.1 Super Admin (SaaS Platform) Full access to the platform. Responsibilities: Tenant management (doctors and clinics) Subscription plans and pricing Feature availability per plan Usage monitoring (users, patients, consultations, IA, storage) Billing and invoicing overview Mercado Pago integration management Tenant activation / suspension Global system settings Security and audit logs 2.2 Tenant Types A) Independent Doctor (Tenant) Acts as the tenant administrator. Internal users: Doctor (admin) Receptionists B) Medical Clinic (Tenant) Clinic is the tenant administrator. Internal users: Clinic admin Doctors Receptionists The clinic controls: Doctor onboarding Staff permissions Agenda policies 3. Multi-Tenancy Strategy Logical isolation using tenant_id (row-level security) Prepared for future migration to database-per-tenant All clinical, billing, and AI data must be tenant-scoped Entities requiring tenant isolation: Users Patients Appointments Medical records Anamnesis Exams Prescriptions Subscriptions Usage logs Files 4. Access Control (RBAC) Role-based access control with granular permissions. Core roles: super_admin tenant_admin doctor receptionist Permissions include: Patient CRUD Appointment scheduling Medical record access Prescription generation IA access Billing visibility 5. Core Modules 5.1 SaaS Core (Super Admin) Tenant lifecycle management Subscription plans (monthly / yearly) Pricing rules Usage quotas: Users Doctors Patients Appointments Storage IA requests/tokens Billing dashboard Revenue reports Mercado Pago webhook handling Tenant blocking on non-payment Feature toggles per plan 5.2 Subscription & Billing (Mercado Pago API) Subscription creation via Mercado Pago API Plan mapping (internal ↔ Mercado Pago) Recurring payments Payment status sync via webhooks Grace period handling Automatic suspension/reactivation Payment history per tenant Invoice/receipt storage 5.3 Branding & Customization Upload clinic/doctor logo Logo used in: Dashboard PDFs (prescriptions, reports) Anamnesis links Optional color theming (future) White-label readiness 5.4 Appointments & Agenda Doctor-level agenda Clinic-wide agenda Appointment status flow Receptionist scheduling Automatic WhatsApp confirmation link generation 5.5 WhatsApp Integration (Anamnesis Flow) Generation of secure public anamnesis link Link tied to: Appointment Patient Tenant Link shared via WhatsApp (copy & paste) Patient fills anamnesis without login Token-based expiration Auto-attach anamnesis to appointment Access logging for compliance 5.6 Patients Complete patient registry Medical history Appointment association Consent management (LGPD) 5.7 Medical Records (EHR) Clinical evolutions Immutable audit trail Doctor attribution Timestamped entries 5.8 Anamnesis (AI-Ready) Configurable templates Manual or AI-assisted filling Structured storage Version control IA usage tracked per tenant 5.9 Exams & Documents Secure file upload Tenant-scoped storage AI-assisted analysis (optional) Storage quota enforcement 5.10 Medical Prescription (PDF) Prescription creation by doctors Customizable templates Doctor signature (typed or image) Clinic/doctor logo embedded PDF generation Patient association Audit log of issuance Future-ready for digital signature 5.11 Tenant Billing Panel Current plan Usage vs limits Payment history Subscription status Upgrade/downgrade Soft-lock on overdue payments 6. AI Governance Layer Dedicated AI service layer Token and request tracking Cost estimation Limits per subscription Automatic throttling Usage logs per tenant 7. Security & Compliance (Medical Grade) Encrypted sensitive data Access logs to patient records Immutable medical logs Soft deletes LGPD compliance: Consent records Data minimization Right to erasure (where legally allowed) Automated backups 8. Observability & Auditing Tenant-level logs Medical audit logs Payment logs IA usage logs Performance monitoring 9. Technical Stack Laravel 11+ Filament Admin Livewire Tailwind CSS Spatie Permissions Queue system (IA / PDFs / webhooks) Mercado Pago API Secure file storage Internal API for future mobile app 10. Future-Ready Extensions Telemedicine Digital signature (ICP-Brasil) WhatsApp API official integration Patient portal White-label SaaS 11. Final Technical Summary This system must be built as a robust, scalable, multi-tenant medical SaaS, combining subscription billing, medical workflows, WhatsApp-based patient interaction, PDF medical documentation, AI-assisted clinical processes, and strong compliance guarantees.