#import <Foundation/Foundation.h>

// ── SAO AUTH Configuration ──────────────────────────────────────────────
static NSString * const kBaseURL    = @"https://saoauth.com/api/client/auth";
static NSString * const kOwnerID    = @"YOUR_OWNER_ID";
static NSString * const kAppSecret  = @"YOUR_APP_SECRET";
static NSString * const kAppVersion = @"1.0";

// ── License Auth ─────────────────────────────────────────────────────────
NSDictionary *saoAuthLicense(NSString *licenseKey, NSString *hwid) {
    NSURL *url = [NSURL URLWithString:kBaseURL];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSString *body = [NSString stringWithFormat:
        @"auth_type=license&key=%@&hwid=%@&ownerid=%@&secret=%@&version=%@",
        licenseKey, hwid, kOwnerID, kAppSecret, kAppVersion];
    req.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];

    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    __block NSDictionary *result = nil;

    NSURLSessionDataTask *task = [[NSURLSession sharedSession]
        dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *resp, NSError *err) {
            if (data) result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            dispatch_semaphore_signal(sem);
        }];
    [task resume];
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    return result;
}

// ── User Login ────────────────────────────────────────────────────────────
NSDictionary *saoAuthUserLogin(NSString *username, NSString *password, NSString *hwid) {
    NSURL *url = [NSURL URLWithString:kBaseURL];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSString *body = [NSString stringWithFormat:
        @"auth_type=user_login&username=%@&password=%@&hwid=%@&ownerid=%@&secret=%@&version=%@",
        username, password, hwid, kOwnerID, kAppSecret, kAppVersion];
    req.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];

    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    __block NSDictionary *result = nil;

    NSURLSessionDataTask *task = [[NSURLSession sharedSession]
        dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *resp, NSError *err) {
            if (data) result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            dispatch_semaphore_signal(sem);
        }];
    [task resume];
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    return result;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDictionary *res = saoAuthLicense(@"YOUR_LICENSE_KEY", @"YOUR_HWID");
        if ([res[@"success"] boolValue]) {
            NSLog(@"Authenticated!");
            NSLog(@"Session token : %@", res[@"session_token"]);
            NSLog(@"Expires at    : %@", res[@"expires_at"] ?: @"Permanent");
            NSLog(@"Sub level     : %@", res[@"sub_level"]);
        } else {
            NSLog(@"Auth failed: %@", res[@"error"]);
        }
    }
    return 0;
}
