public class RiabuAccountToCustomer implements Queueable, Database.AllowsCallouts {
 
    Id accId;
 
    public RiabuAccountToCustomer(Id accountId) {

        this.accId = accountId;

    }
 
    public void execute(QueueableContext qc) {

        try {

            Account acc = [

                SELECT Id, Name, Registration_Number__c, CreatedDate, Days_to_Pay__c

                FROM Account

                WHERE Id = :accId

            ];
 
            System.debug('✅ Account Record to Send: ' + acc.Id);
 
          RIABU_Integration__mdt config = [

                SELECT Access_Token__c, Refresh_Token__c,Endpoint__c

                FROM  RIABU_Integration__mdt 

                WHERE DeveloperName = 'Riabu_Record'

                LIMIT 1

            ];
 
            String token = config.Access_Token__c;
 
            if (String.isBlank(token)) {

                System.debug('❌ No Access Token. Stopping.');

                return;

            }
 
            HttpRequest req = new HttpRequest();

            req.setEndpoint(config.Endpoint__c + '/api/v1/wizard/customer');

            req.setMethod('POST');

            req.setHeader('Authorization', 'Bearer ' + token);

            req.setHeader('Content-Type', 'application/json');
 
            Map<String, Object> payload = new Map<String, Object>{

                'company_name' => acc.Name,

                'company_uen' => acc.Registration_Number__c,

                'customer_since' => acc.CreatedDate.format('yyyy-MM-dd'),

                'agreed_credit_terms_day' => (acc.Days_to_Pay__c == null ? 0 : acc.Days_to_Pay__c)

            };
 
            req.setBody(JSON.serialize(payload));
 
            Http http = new Http();

            HttpResponse res = http.send(req);
 
            System.debug('🌍 Riabu Response Code: ' + res.getStatusCode());
 
 
        } catch (Exception e) {

            System.debug('❌ Error: ' + e.getMessage());

        }

    }

}