-- 1. Remove leftover test transactions
alter table public.journal_entries disable trigger user;
alter table public.journal_lines disable trigger user;

delete from public.refunds;
delete from public.credit_notes;
delete from public.payment_allocations;
delete from public.payments;
delete from public.invoice_items;
delete from public.invoice_approvals;
delete from public.invoice_reminders;
delete from public.invoice_emails;
delete from public.invoices;
delete from public.expense_claim_approvals;
delete from public.expense_claims;
delete from public.expenses;
delete from public.booking_costs;
delete from public.booking_approvals;
delete from public.bookings;
delete from public.journal_lines;
delete from public.journal_entries;
delete from public.bank_transactions;
delete from public.audit_logs;

alter table public.journal_entries enable trigger user;
alter table public.journal_lines enable trigger user;

update public.number_sequences set next_value = 1;

-- 2. Replace test master data with real studio data
delete from public.customers where name in ('Ledger Test Customer','Trial Client TRIAL-6140');

insert into public.customers (customer_code, name, company_name, email, phone, address, city, status)
values ('CUS-1001', 'Rupali Weddings Ltd.', 'Rupali Weddings Ltd.', 'accounts@rupaliweddings.com', '+8801711234567', 'House 42, Road 11, Banani', 'Dhaka', 'active');

update public.rooms set name = 'Studio A - Main Floor', room_type = coalesce(room_type,'Photography'), hourly_rate = 2500
where name = 'Test Live Room A';

insert into public.services (code, name, unit, rate, tax_rate, revenue_account_id, cost, cost_account_id, is_active)
select 'SVC-HOURLY', 'Studio Rental - Hourly', 'hour', 2500, 0,
       (select id from public.accounts where code = '4010'), 800,
       (select id from public.accounts where code = '6100'), true
where not exists (select 1 from public.services where code = 'SVC-HOURLY');