begin;
select plan(97); -- Total number of tests

-- ============================================================================
-- Table Existence Tests
-- ============================================================================

SELECT has_table('public', 'profiles', 'profiles table should exist');
SELECT has_table('public', 'feedbacks', 'feedbacks table should exist');
SELECT has_table('public', 'tablos', 'tablos table should exist');
SELECT has_table('public', 'tablo_access', 'tablo_access table should exist');
SELECT has_table('public', 'tablo_invites', 'tablo_invites table should exist');
SELECT has_table('public', 'events', 'events table should exist');
SELECT has_table('public', 'notes', 'notes table should exist');
SELECT has_table('public', 'shared_notes', 'shared_notes table should exist');
SELECT has_table('public', 'note_access', 'note_access table should exist');

-- ============================================================================
-- Feedbacks Table Structure
-- ============================================================================

SELECT has_column('public', 'feedbacks', 'id', 'feedbacks should have id column');
SELECT has_column('public', 'feedbacks', 'fd_type', 'feedbacks should have fd_type column');
SELECT has_column('public', 'feedbacks', 'user_id', 'feedbacks should have user_id column');
SELECT has_column('public', 'feedbacks', 'message', 'feedbacks should have message column');
SELECT has_column('public', 'feedbacks', 'created_at', 'feedbacks should have created_at column');

SELECT col_type_is('public', 'feedbacks', 'fd_type', 'character varying(20)', 'feedbacks.fd_type should be varchar(20)');
SELECT col_type_is('public', 'feedbacks', 'user_id', 'uuid', 'feedbacks.user_id should be uuid');
SELECT col_type_is('public', 'feedbacks', 'message', 'text', 'feedbacks.message should be text');

-- ============================================================================
-- Tablos Table Structure
-- ============================================================================

SELECT has_column('public', 'tablos', 'id', 'tablos should have id column');
SELECT has_column('public', 'tablos', 'owner_id', 'tablos should have owner_id column');
SELECT has_column('public', 'tablos', 'name', 'tablos should have name column');
SELECT has_column('public', 'tablos', 'image', 'tablos should have image column');
SELECT has_column('public', 'tablos', 'color', 'tablos should have color column');
SELECT has_column('public', 'tablos', 'status', 'tablos should have status column');
SELECT has_column('public', 'tablos', 'position', 'tablos should have position column');
SELECT has_column('public', 'tablos', 'created_at', 'tablos should have created_at column');
SELECT has_column('public', 'tablos', 'deleted_at', 'tablos should have deleted_at column');

SELECT col_type_is('public', 'tablos', 'owner_id', 'uuid', 'tablos.owner_id should be uuid');
SELECT col_type_is('public', 'tablos', 'name', 'character varying(255)', 'tablos.name should be varchar(255)');
SELECT col_type_is('public', 'tablos', 'status', 'character varying(20)', 'tablos.status should be varchar(20)');
SELECT col_type_is('public', 'tablos', 'position', 'integer', 'tablos.position should be integer');

SELECT col_not_null('public', 'tablos', 'owner_id', 'tablos.owner_id should be NOT NULL');
SELECT col_not_null('public', 'tablos', 'name', 'tablos.name should be NOT NULL');
-- Note: status is now nullable and computed from etapes
SELECT col_is_null('public', 'tablos', 'status', 'tablos.status is now nullable (deprecated)');

SELECT col_has_default('public', 'tablos', 'status', 'tablos.status should have default');
SELECT col_has_default('public', 'tablos', 'position', 'tablos.position should have default');

-- ============================================================================
-- Tablo Access Table Structure
-- ============================================================================

SELECT has_column('public', 'tablo_access', 'id', 'tablo_access should have id column');
SELECT has_column('public', 'tablo_access', 'tablo_id', 'tablo_access should have tablo_id column');
SELECT has_column('public', 'tablo_access', 'user_id', 'tablo_access should have user_id column');
SELECT has_column('public', 'tablo_access', 'granted_by', 'tablo_access should have granted_by column');
SELECT has_column('public', 'tablo_access', 'is_active', 'tablo_access should have is_active column');
SELECT has_column('public', 'tablo_access', 'is_admin', 'tablo_access should have is_admin column');
SELECT has_column('public', 'tablo_access', 'created_at', 'tablo_access should have created_at column');

SELECT col_type_is('public', 'tablo_access', 'tablo_id', 'text', 'tablo_access.tablo_id should be text');
SELECT col_type_is('public', 'tablo_access', 'user_id', 'uuid', 'tablo_access.user_id should be uuid');
SELECT col_type_is('public', 'tablo_access', 'is_active', 'boolean', 'tablo_access.is_active should be boolean');
SELECT col_type_is('public', 'tablo_access', 'is_admin', 'boolean', 'tablo_access.is_admin should be boolean');

-- ============================================================================
-- Tablo Invites Table Structure
-- ============================================================================

SELECT has_column('public', 'tablo_invites', 'id', 'tablo_invites should have id column');
SELECT has_column('public', 'tablo_invites', 'tablo_id', 'tablo_invites should have tablo_id column');
SELECT has_column('public', 'tablo_invites', 'invited_email', 'tablo_invites should have invited_email column');
SELECT has_column('public', 'tablo_invites', 'invited_by', 'tablo_invites should have invited_by column');
SELECT has_column('public', 'tablo_invites', 'invite_token', 'tablo_invites should have invite_token column');

SELECT col_type_is('public', 'tablo_invites', 'tablo_id', 'text', 'tablo_invites.tablo_id should be text');
SELECT col_type_is('public', 'tablo_invites', 'invited_email', 'character varying(255)', 'tablo_invites.invited_email should be varchar(255)');
SELECT col_type_is('public', 'tablo_invites', 'invited_by', 'uuid', 'tablo_invites.invited_by should be uuid');

-- ============================================================================
-- Events Table Structure
-- ============================================================================

SELECT has_column('public', 'events', 'id', 'events should have id column');
SELECT has_column('public', 'events', 'tablo_id', 'events should have tablo_id column');
SELECT has_column('public', 'events', 'title', 'events should have title column');
SELECT has_column('public', 'events', 'description', 'events should have description column');
SELECT has_column('public', 'events', 'start_date', 'events should have start_date column');
SELECT has_column('public', 'events', 'start_time', 'events should have start_time column');
SELECT has_column('public', 'events', 'end_time', 'events should have end_time column');
SELECT has_column('public', 'events', 'created_by', 'events should have created_by column');
SELECT has_column('public', 'events', 'created_at', 'events should have created_at column');
SELECT has_column('public', 'events', 'deleted_at', 'events should have deleted_at column');

SELECT col_type_is('public', 'events', 'id', 'text', 'events.id should be text');
SELECT col_type_is('public', 'events', 'tablo_id', 'text', 'events.tablo_id should be text');
SELECT col_type_is('public', 'events', 'title', 'character varying(255)', 'events.title should be varchar(255)');
SELECT col_type_is('public', 'events', 'start_date', 'date', 'events.start_date should be date');
SELECT col_type_is('public', 'events', 'start_time', 'time without time zone', 'events.start_time should be time');
SELECT col_type_is('public', 'events', 'created_by', 'uuid', 'events.created_by should be uuid');

-- ============================================================================
-- Notes Table Structure
-- ============================================================================

SELECT has_column('public', 'notes', 'id', 'notes should have id column');
SELECT has_column('public', 'notes', 'title', 'notes should have title column');
SELECT has_column('public', 'notes', 'content', 'notes should have content column');
SELECT has_column('public', 'notes', 'user_id', 'notes should have user_id column');
SELECT has_column('public', 'notes', 'created_at', 'notes should have created_at column');
SELECT has_column('public', 'notes', 'updated_at', 'notes should have updated_at column');
SELECT has_column('public', 'notes', 'deleted_at', 'notes should have deleted_at column');

SELECT col_type_is('public', 'notes', 'id', 'text', 'notes.id should be text');
SELECT col_type_is('public', 'notes', 'title', 'character varying(255)', 'notes.title should be varchar(255)');
SELECT col_type_is('public', 'notes', 'content', 'text', 'notes.content should be text');
SELECT col_type_is('public', 'notes', 'user_id', 'uuid', 'notes.user_id should be uuid');

-- ============================================================================
-- Shared Notes Table Structure
-- ============================================================================

SELECT has_column('public', 'shared_notes', 'note_id', 'shared_notes should have note_id column');
SELECT has_column('public', 'shared_notes', 'user_id', 'shared_notes should have user_id column');
SELECT has_column('public', 'shared_notes', 'is_public', 'shared_notes should have is_public column');
SELECT has_column('public', 'shared_notes', 'created_at', 'shared_notes should have created_at column');

SELECT col_type_is('public', 'shared_notes', 'note_id', 'text', 'shared_notes.note_id should be text');
SELECT col_type_is('public', 'shared_notes', 'user_id', 'uuid', 'shared_notes.user_id should be uuid');
SELECT col_type_is('public', 'shared_notes', 'is_public', 'boolean', 'shared_notes.is_public should be boolean');

-- ============================================================================
-- Note Access Table Structure
-- ============================================================================

SELECT has_column('public', 'note_access', 'id', 'note_access should have id column');
SELECT has_column('public', 'note_access', 'note_id', 'note_access should have note_id column');
SELECT has_column('public', 'note_access', 'user_id', 'note_access should have user_id column');
SELECT has_column('public', 'note_access', 'tablo_id', 'note_access should have tablo_id column');
SELECT has_column('public', 'note_access', 'is_active', 'note_access should have is_active column');

SELECT col_type_is('public', 'note_access', 'note_id', 'text', 'note_access.note_id should be text');
SELECT col_type_is('public', 'note_access', 'user_id', 'uuid', 'note_access.user_id should be uuid');
SELECT col_type_is('public', 'note_access', 'tablo_id', 'text', 'note_access.tablo_id should be text');
SELECT col_type_is('public', 'note_access', 'is_active', 'boolean', 'note_access.is_active should be boolean');

select * from finish();
rollback;

