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

<cfscript>
businessId = 27;
response = { "OK": false };

try {
    // Big Dean's actual info
    // Address: 1615 Ocean Front Walk, Santa Monica, CA 90401
    // Phone: (310) 393-2666
    // Hours: Mon-Thu: 11am-10pm, Fri-Sat: 11am-11pm, Sun: 11am-10pm

    // Get California StateID
    qState = queryExecute("SELECT ID FROM tt_States WHERE Abbreviation = 'CA' LIMIT 1");
    stateId = qState.recordCount > 0 ? qState.ID : 5; // Default to 5 if not found

    // Check if Big Dean's already has an address
    existingAddr = queryExecute("
        SELECT ID FROM Addresses
        WHERE BusinessID = :bizId AND UserID = 0
    ", { bizId: businessId });

    if (existingAddr.recordCount == 0) {
        // Insert new address
        queryExecute("
            INSERT INTO Addresses (UserID, BusinessID, AddressTypeID, Line1, City, StateID, ZIPCode, IsDeleted, AddedOn)
            VALUES (0, :bizId, '2', :line1, :city, :stateId, :zip, 0, NOW())
        ", {
            bizId: businessId,
            line1: "1615 Ocean Front Walk",
            city: "Santa Monica",
            stateId: stateId,
            zip: "90401"
        });
        response["ADDRESS_ACTION"] = "inserted";
    } else {
        // Update existing address
        queryExecute("
            UPDATE Addresses
            SET Line1 = :line1, City = :city, StateID = :stateId, ZIPCode = :zip
            WHERE BusinessID = :bizId AND UserID = 0
        ", {
            bizId: businessId,
            line1: "1615 Ocean Front Walk",
            city: "Santa Monica",
            stateId: stateId,
            zip: "90401"
        });
        response["ADDRESS_ACTION"] = "updated";
    }

    // Check existing hours for this business
    existingHours = queryExecute("
        SELECT COUNT(*) as cnt FROM Hours WHERE BusinessID = :bizId
    ", { bizId: businessId });

    if (existingHours.cnt == 0) {
        // Insert hours for each day
        // Days: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday
        // Mon-Thu: 11am-10pm (days 2-5)
        // Fri-Sat: 11am-11pm (days 6-7)
        // Sun: 11am-10pm (day 1)

        // Sunday (1): 11am-10pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 1, '11:00:00', '22:00:00')", { bizId: businessId });

        // Monday (2): 11am-10pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 2, '11:00:00', '22:00:00')", { bizId: businessId });

        // Tuesday (3): 11am-10pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 3, '11:00:00', '22:00:00')", { bizId: businessId });

        // Wednesday (4): 11am-10pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 4, '11:00:00', '22:00:00')", { bizId: businessId });

        // Thursday (5): 11am-10pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 5, '11:00:00', '22:00:00')", { bizId: businessId });

        // Friday (6): 11am-11pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 6, '11:00:00', '23:00:00')", { bizId: businessId });

        // Saturday (7): 11am-11pm
        queryExecute("INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime) VALUES (:bizId, 7, '11:00:00', '23:00:00')", { bizId: businessId });

        response["HOURS_ACTION"] = "inserted 7 days";
    } else {
        // Update existing hours
        // Mon-Thu: 11am-10pm
        queryExecute("UPDATE Hours SET OpenTime = '11:00:00', ClosingTime = '22:00:00' WHERE BusinessID = :bizId AND DayID IN (1, 2, 3, 4, 5)", { bizId: businessId });
        // Fri-Sat: 11am-11pm
        queryExecute("UPDATE Hours SET OpenTime = '11:00:00', ClosingTime = '23:00:00' WHERE BusinessID = :bizId AND DayID IN (6, 7)", { bizId: businessId });
        response["HOURS_ACTION"] = "updated";
    }

    // Update phone on Businesses table (if column exists)
    try {
        queryExecute("UPDATE Businesses SET Phone = :phone WHERE ID = :bizId", {
            phone: "(310) 393-2666",
            bizId: businessId
        });
        response["PHONE_ACTION"] = "updated";
    } catch (any e) {
        response["PHONE_ACTION"] = "column may not exist: " & e.message;
    }

    // Verify the data
    address = queryExecute("
        SELECT a.*, s.Abbreviation
        FROM Addresses a
        LEFT JOIN tt_States s ON s.ID = a.StateID
        WHERE a.BusinessID = :bizId AND a.UserID = 0
    ", { bizId: businessId });

    hours = queryExecute("
        SELECT h.*, d.Name AS DayName
        FROM Hours h
        JOIN tt_Days d ON d.ID = h.DayID
        WHERE h.BusinessID = :bizId
        ORDER BY h.DayID
    ", { bizId: businessId });

    response["OK"] = true;
    response["BUSINESS_ID"] = businessId;
    response["ADDRESS"] = address.recordCount > 0 ? {
        "line1": address.Line1,
        "city": address.City,
        "state": address.Abbreviation,
        "zip": address.ZIPCode
    } : "not found";

    hoursArr = [];
    for (h in hours) {
        arrayAppend(hoursArr, {
            "day": h.DayName,
            "open": timeFormat(h.OpenTime, "h:mm tt"),
            "close": timeFormat(h.ClosingTime, "h:mm tt")
        });
    }
    response["HOURS"] = hoursArr;

} catch (any e) {
    response["ERROR"] = e.message;
    response["DETAIL"] = e.detail;
}

writeOutput(serializeJSON(response));
</cfscript>
