<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">

<cfscript>
// Update Big Dean's business info
businessId = 27;

// Big Dean's actual info
phone = "(310) 393-2666";
hours = "Mon-Thu: 11am-10pm, Fri-Sat: 11am-11pm, Sun: 11am-10pm";

try {
    // Update phone and hours on Businesses table
    queryTimed("
        UPDATE Businesses
        SET Phone = :phone,
            Hours = :hours
        WHERE ID = :bizId
    ", {
        phone: phone,
        hours: hours,
        bizId: businessId
    }, { datasource: "payfrit" });

    // Update or insert address in Addresses table
    qAddr = queryTimed("
        SELECT ID FROM Addresses
        WHERE BusinessID = :bizId AND IsDeleted = 0
        LIMIT 1
    ", { bizId: businessId }, { datasource: "payfrit" });

    if (qAddr.recordCount > 0) {
        queryTimed("
            UPDATE Addresses
            SET Line1 = :line1, City = :city, ZIPCode = :zip
            WHERE ID = :addrId
        ", {
            line1: "1615 Ocean Front Walk",
            city: "Santa Monica",
            zip: "90401",
            addrId: qAddr.ID
        }, { datasource: "payfrit" });
    } else {
        queryTimed("
            INSERT INTO Addresses (BusinessID, UserID, AddressTypeID, Line1, City, ZIPCode, AddedOn)
            VALUES (:bizId, 0, 'business', :line1, :city, :zip, NOW())
        ", {
            bizId: businessId,
            line1: "1615 Ocean Front Walk",
            city: "Santa Monica",
            zip: "90401"
        }, { datasource: "payfrit" });
    }

    // Get updated record
    updated = queryTimed("
        SELECT ID, Name, Phone, Hours
        FROM Businesses
        WHERE ID = :bizId
    ", { bizId: businessId }, { datasource: "payfrit" });

    writeOutput(serializeJSON({
        "OK": true,
        "MESSAGE": "Updated Big Dean's info",
        "BUSINESS": {
            "BusinessID": updated.ID,
            "Name": updated.Name,
            "Phone": updated.Phone,
            "Hours": updated.Hours
        }
    }));

} catch (any e) {
    writeOutput(serializeJSON({
        "OK": false,
        "ERROR": e.message,
        "DETAIL": e.detail
    }));
}
</cfscript>
