-- UI seed data for Playwright (dev database only)
-- Non-destructive: insert/update required rows without truncating tables.

insert into users(email, display_name, password_hash, system_role, created_at, last_login_at)
values
  ('owner1@fiddy.dev', 'Owner One', '__PASSWORD_HASH__', 'USER', now() - interval '10 days', now() - interval '1 day'),
  ('admin1@fiddy.dev', 'Admin One', '__PASSWORD_HASH__', 'USER', now() - interval '9 days', now() - interval '2 days'),
  ('member1@fiddy.dev', 'Member One', '__PASSWORD_HASH__', 'USER', now() - interval '8 days', now() - interval '3 days'),
  ('requester1@fiddy.dev', 'Requester One', '__PASSWORD_HASH__', 'USER', now() - interval '7 days', null),
  ('nogroup@fiddy.dev', 'No Group', '__PASSWORD_HASH__', 'USER', now() - interval '6 days', null),
  ('member2@fiddy.dev', 'Member Two', '__PASSWORD_HASH__', 'USER', now() - interval '5 days', null)
on conflict (email) do update
set display_name = excluded.display_name,
    password_hash = excluded.password_hash,
    system_role = excluded.system_role,
    last_login_at = excluded.last_login_at;

insert into groups(name, invite_code, created_by, created_at)
values
  ('Alpha Household', 'ALPHADEV', (select id from users where email='owner1@fiddy.dev'), now() - interval '9 days'),
  ('Beta Office', 'BETADEV', (select id from users where email='admin1@fiddy.dev'), now() - interval '8 days'),
  ('Gamma Club', 'GAMMADEV', (select id from users where email='member1@fiddy.dev'), now() - interval '7 days')
on conflict (invite_code) do update
set name = excluded.name,
    created_by = excluded.created_by;

insert into group_settings(group_id, allow_member_tag_manage, join_policy, updated_at)
values
  ((select id from groups where invite_code='ALPHADEV'), true, 'AUTO_ACCEPT', now() - interval '2 days'),
  ((select id from groups where invite_code='BETADEV'), false, 'APPROVAL_REQUIRED', now() - interval '2 days'),
  ((select id from groups where invite_code='GAMMADEV'), false, 'NOT_ACCEPTING', now() - interval '2 days')
on conflict (group_id) do update
set allow_member_tag_manage = excluded.allow_member_tag_manage,
    join_policy = excluded.join_policy,
    updated_at = excluded.updated_at;

insert into group_members(group_id, user_id, role, created_at, last_active_at)
values
  ((select id from groups where invite_code='ALPHADEV'), (select id from users where email='owner1@fiddy.dev'), 'GROUP_OWNER', now() - interval '9 days', now() - interval '1 day'),
  ((select id from groups where invite_code='ALPHADEV'), (select id from users where email='admin1@fiddy.dev'), 'GROUP_ADMIN', now() - interval '9 days', now() - interval '1 day'),
  ((select id from groups where invite_code='ALPHADEV'), (select id from users where email='member1@fiddy.dev'), 'MEMBER', now() - interval '9 days', now() - interval '2 days'),
  ((select id from groups where invite_code='BETADEV'), (select id from users where email='admin1@fiddy.dev'), 'GROUP_OWNER', now() - interval '8 days', now() - interval '2 days'),
  ((select id from groups where invite_code='BETADEV'), (select id from users where email='member2@fiddy.dev'), 'MEMBER', now() - interval '8 days', now() - interval '2 days'),
  ((select id from groups where invite_code='GAMMADEV'), (select id from users where email='member1@fiddy.dev'), 'GROUP_OWNER', now() - interval '7 days', now() - interval '3 days'),
  ((select id from groups where invite_code='GAMMADEV'), (select id from users where email='admin1@fiddy.dev'), 'MEMBER', now() - interval '7 days', now() - interval '3 days')
on conflict (group_id, user_id) do update
set role = excluded.role,
    last_active_at = excluded.last_active_at;

insert into user_settings(user_id, data, updated_at)
values
  ((select id from users where email='owner1@fiddy.dev'), jsonb_build_object('activeGroupId', (select id from groups where invite_code='ALPHADEV')), now() - interval '1 day'),
  ((select id from users where email='admin1@fiddy.dev'), jsonb_build_object('activeGroupId', (select id from groups where invite_code='BETADEV')), now() - interval '1 day'),
  ((select id from users where email='member1@fiddy.dev'), jsonb_build_object('activeGroupId', (select id from groups where invite_code='ALPHADEV')), now() - interval '1 day')
on conflict (user_id) do update
set data = user_settings.data || excluded.data,
    updated_at = excluded.updated_at;

insert into group_join_requests(group_id, user_id, status, created_at, updated_at)
select
  (select id from groups where invite_code='BETADEV'),
  (select id from users where email='requester1@fiddy.dev'),
  'PENDING',
  now() - interval '1 day',
  now() - interval '1 day'
where not exists (
  select 1 from group_join_requests
  where group_id=(select id from groups where invite_code='BETADEV')
    and user_id=(select id from users where email='requester1@fiddy.dev')
    and status='PENDING'
);

insert into group_invite_links(group_id, created_by, token, policy, single_use, expires_at, used_at, revoked_at, created_at)
values
  ((select id from groups where invite_code='ALPHADEV'), (select id from users where email='owner1@fiddy.dev'), 'INVITE-ALPHA-1', 'AUTO_ACCEPT', false, now() + interval '3 days', null, null, now() - interval '1 day'),
  ((select id from groups where invite_code='BETADEV'), (select id from users where email='admin1@fiddy.dev'), 'INVITE-BETA-EXPIRED', 'APPROVAL_REQUIRED', false, now() - interval '1 day', null, null, now() - interval '4 days'),
  ((select id from groups where invite_code='GAMMADEV'), (select id from users where email='member1@fiddy.dev'), 'INVITE-GAMMA-REVOKED', 'NOT_ACCEPTING', false, now() + interval '5 days', null, now() - interval '2 hours', now() - interval '2 days'),
  ((select id from groups where invite_code='ALPHADEV'), (select id from users where email='owner1@fiddy.dev'), 'INVITE-ALPHA-SINGLE', 'AUTO_ACCEPT', true, now() + interval '2 days', now() - interval '1 hour', null, now() - interval '2 days')
on conflict (token) do update
set group_id = excluded.group_id,
    created_by = excluded.created_by,
    policy = excluded.policy,
    single_use = excluded.single_use,
    expires_at = excluded.expires_at,
    used_at = excluded.used_at,
    revoked_at = excluded.revoked_at,
    created_at = excluded.created_at;

insert into group_audit_log(group_id, actor_user_id, actor_role, event_type, request_id, ip, user_agent, success, error_code, metadata, created_at)
select
  (select id from groups where invite_code='BETADEV'),
  (select id from users where email='admin1@fiddy.dev'),
  'GROUP_OWNER',
  'INVITE_CREATED',
  'seed-req-1',
  null,
  null,
  true,
  null,
  jsonb_build_object('inviteLast4', 'XPIR'),
  now() - interval '1 day'
where not exists (
  select 1 from group_audit_log where request_id='seed-req-1' and event_type='INVITE_CREATED'
);

insert into group_audit_log(group_id, actor_user_id, actor_role, event_type, request_id, ip, user_agent, success, error_code, metadata, created_at)
select
  (select id from groups where invite_code='ALPHADEV'),
  (select id from users where email='owner1@fiddy.dev'),
  'GROUP_OWNER',
  'GROUP_RENAMED',
  'seed-req-2',
  null,
  null,
  true,
  null,
  jsonb_build_object('name', 'Alpha Household'),
  now() - interval '2 days'
where not exists (
  select 1 from group_audit_log where request_id='seed-req-2' and event_type='GROUP_RENAMED'
);

insert into tags(group_id, name, created_by, created_at)
select g.id, 'Food', u.id, now() - interval '7 days'
from groups g, users u
where g.invite_code='ALPHADEV' and u.email='owner1@fiddy.dev'
  and not exists (select 1 from tags t where t.group_id=g.id and lower(t.name)=lower('Food'));

insert into tags(group_id, name, created_by, created_at)
select g.id, 'Travel', u.id, now() - interval '7 days'
from groups g, users u
where g.invite_code='ALPHADEV' and u.email='owner1@fiddy.dev'
  and not exists (select 1 from tags t where t.group_id=g.id and lower(t.name)=lower('Travel'));

insert into tags(group_id, name, created_by, created_at)
select g.id, 'Utilities', u.id, now() - interval '6 days'
from groups g, users u
where g.invite_code='ALPHADEV' and u.email='admin1@fiddy.dev'
  and not exists (select 1 from tags t where t.group_id=g.id and lower(t.name)=lower('Utilities'));

insert into tags(group_id, name, created_by, created_at)
select g.id, 'Office', u.id, now() - interval '6 days'
from groups g, users u
where g.invite_code='BETADEV' and u.email='admin1@fiddy.dev'
  and not exists (select 1 from tags t where t.group_id=g.id and lower(t.name)=lower('Office'));

insert into receipts(content_type, bytes, created_by, created_at)
select 'text/plain', convert_to('RECEIPT: ALPHA-ONE', 'UTF8'), u.id, now() - interval '3 days'
from users u
where u.email='owner1@fiddy.dev'
  and not exists (select 1 from receipts r where r.created_by=u.id and r.bytes=convert_to('RECEIPT: ALPHA-ONE', 'UTF8'));

insert into receipts(content_type, bytes, created_by, created_at)
select 'text/plain', convert_to('RECEIPT: ALPHA-TWO', 'UTF8'), u.id, now() - interval '2 days'
from users u
where u.email='owner1@fiddy.dev'
  and not exists (select 1 from receipts r where r.created_by=u.id and r.bytes=convert_to('RECEIPT: ALPHA-TWO', 'UTF8'));

insert into receipts(content_type, bytes, created_by, created_at)
select 'text/plain', convert_to('RECEIPT: BETA-ONE', 'UTF8'), u.id, now() - interval '4 days'
from users u
where u.email='admin1@fiddy.dev'
  and not exists (select 1 from receipts r where r.created_by=u.id and r.bytes=convert_to('RECEIPT: BETA-ONE', 'UTF8'));

insert into entries(group_id, created_by, amount_dollars, occurred_at, necessity, purchase_type, notes, receipt_id, created_at)
select
  (select id from groups where invite_code='ALPHADEV'),
  (select id from users where email='owner1@fiddy.dev'),
  12.50,
  current_date - 2,
  'NECESSARY',
  'Food',
  'Groceries',
  (select id from receipts where bytes=convert_to('RECEIPT: ALPHA-ONE', 'UTF8') limit 1),
  now() - interval '2 days'
where not exists (
  select 1 from entries
  where group_id=(select id from groups where invite_code='ALPHADEV')
    and created_by=(select id from users where email='owner1@fiddy.dev')
    and amount_dollars=12.50
    and occurred_at=current_date - 2
    and purchase_type='Food'
    and notes='Groceries'
);

insert into entries(group_id, created_by, amount_dollars, occurred_at, necessity, purchase_type, notes, receipt_id, created_at)
select
  (select id from groups where invite_code='ALPHADEV'),
  (select id from users where email='owner1@fiddy.dev'),
  44.10,
  current_date - 1,
  'BOTH',
  'Travel',
  'Train ticket',
  (select id from receipts where bytes=convert_to('RECEIPT: ALPHA-TWO', 'UTF8') limit 1),
  now() - interval '1 day'
where not exists (
  select 1 from entries
  where group_id=(select id from groups where invite_code='ALPHADEV')
    and created_by=(select id from users where email='owner1@fiddy.dev')
    and amount_dollars=44.10
    and occurred_at=current_date - 1
    and purchase_type='Travel'
    and notes='Train ticket'
);

insert into entries(group_id, created_by, amount_dollars, occurred_at, necessity, purchase_type, notes, receipt_id, created_at)
select
  (select id from groups where invite_code='ALPHADEV'),
  (select id from users where email='admin1@fiddy.dev'),
  9.99,
  current_date,
  'UNNECESSARY',
  'Snacks',
  'No receipt',
  null,
  now()
where not exists (
  select 1 from entries
  where group_id=(select id from groups where invite_code='ALPHADEV')
    and created_by=(select id from users where email='admin1@fiddy.dev')
    and amount_dollars=9.99
    and occurred_at=current_date
    and purchase_type='Snacks'
    and notes='No receipt'
);

insert into entries(group_id, created_by, amount_dollars, occurred_at, necessity, purchase_type, notes, receipt_id, created_at)
select
  (select id from groups where invite_code='BETADEV'),
  (select id from users where email='admin1@fiddy.dev'),
  120.00,
  current_date - 3,
  'NECESSARY',
  'Office',
  'Printer supplies',
  (select id from receipts where bytes=convert_to('RECEIPT: BETA-ONE', 'UTF8') limit 1),
  now() - interval '3 days'
where not exists (
  select 1 from entries
  where group_id=(select id from groups where invite_code='BETADEV')
    and created_by=(select id from users where email='admin1@fiddy.dev')
    and amount_dollars=120.00
    and occurred_at=current_date - 3
    and purchase_type='Office'
    and notes='Printer supplies'
);

insert into entries(group_id, created_by, amount_dollars, occurred_at, necessity, purchase_type, notes, receipt_id, created_at)
select
  (select id from groups where invite_code='BETADEV'),
  (select id from users where email='member2@fiddy.dev'),
  15.00,
  current_date - 5,
  'NECESSARY',
  'Office',
  null,
  null,
  now() - interval '5 days'
where not exists (
  select 1 from entries
  where group_id=(select id from groups where invite_code='BETADEV')
    and created_by=(select id from users where email='member2@fiddy.dev')
    and amount_dollars=15.00
    and occurred_at=current_date - 5
    and purchase_type='Office'
    and notes is null
);

insert into entry_tags(entry_id, tag_id, created_at)
select s.id, t.id, now() - interval '2 days'
from entries s
join tags t on t.group_id = s.group_id and lower(t.name)=lower('Food')
where s.amount_dollars=12.50 and s.purchase_type='Food'
  and not exists (select 1 from entry_tags st where st.entry_id=s.id and st.tag_id=t.id);

insert into entry_tags(entry_id, tag_id, created_at)
select s.id, t.id, now() - interval '1 day'
from entries s
join tags t on t.group_id = s.group_id and lower(t.name)=lower('Travel')
where s.amount_dollars=44.10 and s.purchase_type='Travel'
  and not exists (select 1 from entry_tags st where st.entry_id=s.id and st.tag_id=t.id);

insert into entry_tags(entry_id, tag_id, created_at)
select s.id, t.id, now() - interval '3 days'
from entries s
join tags t on t.group_id = s.group_id and lower(t.name)=lower('Office')
where s.amount_dollars=120.00 and s.purchase_type='Office'
  and not exists (select 1 from entry_tags st where st.entry_id=s.id and st.tag_id=t.id);

select setval(pg_get_serial_sequence('users', 'id'), (select coalesce(max(id), 0) from users));
select setval(pg_get_serial_sequence('groups', 'id'), (select coalesce(max(id), 0) from groups));
select setval(pg_get_serial_sequence('group_join_requests', 'id'), (select coalesce(max(id), 0) from group_join_requests));
select setval(pg_get_serial_sequence('group_invite_links', 'id'), (select coalesce(max(id), 0) from group_invite_links));
select setval(pg_get_serial_sequence('group_audit_log', 'id'), (select coalesce(max(id), 0) from group_audit_log));
select setval(pg_get_serial_sequence('tags', 'id'), (select coalesce(max(id), 0) from tags));
select setval(pg_get_serial_sequence('receipts', 'id'), (select coalesce(max(id), 0) from receipts));
select setval(pg_get_serial_sequence('entries', 'id'), (select coalesce(max(id), 0) from entries));
