drop table if exists members cascade;
create table members(
	member_id serial not null,
	user_id integer references users( user_id ),
	member_username varchar(64) not null,
	member_aliasname varchar(128) not null,
	member_type varchar(16) not null,
	current_status varchar(32) not null,
	use_email varchar(8) default 'own',

	zip varchar(16),
	country_id varchar(2) references countries( country_id ),
	county_id integer references counties( county_id ),
	city varchar(64),
	address varchar(96),
	is_address_visible boolean default false,
	phone varchar(128),
	is_phone_visible boolean default false,
	fax varchar(64),
	is_fax_visible boolean default false,
	url varchar(128),

	preferred_paymode varchar( 16 ) default( 'directdebit' ),

	-- billing stuff
	billing_name varchar(128),
	billing_zip varchar(16),
	billing_country_id varchar(2) references countries( country_id ),
	billing_county_id integer references counties( county_id ),
	billing_city varchar(64),
	billing_address varchar(96),
	-- billing stuff
	shipping_name varchar(128),
	shipping_zip varchar(16),
	shipping_country_id varchar(2) references countries( country_id ),
	shipping_county_id integer references counties( county_id ),
	shipping_city varchar(64),
	shipping_address varchar(96),

	contact_name varchar(96),
	contact_email varchar(128),
	-- contact_phone varchar(128),

	haspicture boolean default false,

	notifications varchar(8) default 'all',
	status_senttstamp timestamp without time zone,
	paid_tstamp timestamp without time zone,
	expires_tstamp timestamp without time zone,

	custom_language varchar(255),
	custom_membership varchar(255),
	custom_cat varchar(255),

	npublic_comment text,

	is_deleted boolean default false,
	is_dirty boolean default false,

	umod_tstamp timestamp without time zone,
	amod_tstamp timestamp without time zone,

	ipaddress varchar(16),
	useragent varchar(255),

	capacity integer default 100,
	forditmail boolean default false,
	oldid integer,
	constraint members_member_id_pkey primary key( member_id )
);

comment on table members is 'storage of members';
comment on column members.member_id is 'unique id of member table';
comment on column members.user_id is 'reference to users table';
comment on column members.member_username is 'username of member';
comment on column members.member_aliasname is 'aliasname of user';
comment on column members.member_type is 'type of the member (agency/freelancer)';
comment on column members.current_status is 'status of the member';
comment on column members.use_email is 'use of mail address (own/fordit/both)';
comment on column members.zip is 'zipcode of user';
comment on column members.country_id is 'reference id to countries table';
comment on column members.county_id is 'reference id to counties table';
comment on column members.city is 'city of user';
comment on column members.address is 'address of user';
comment on column members.is_address_visible is 'is address visible on webpage';
comment on column members.phone is 'phone contacts of user';
comment on column members.is_phone_visible is 'is phone visible on webpage';
comment on column members.fax is 'fax of the user';
comment on column members.is_fax_visible is 'boolean flag if fax is visible';
comment on column members.url is 'url of the user';

comment on column members.preferred_paymode is 'preferred paymode (directdebit/postalcheque/creditcard)';
comment on column members.billing_name is 'billing name of member';
comment on column members.billing_zip is 'billing zipcode of member';
comment on column members.billing_country_id is 'billing country id of member';
comment on column members.billing_county_id is 'billing county id of member';
comment on column members.billing_city is 'billing city of member';
comment on column members.billing_address is 'billing address of member';

comment on column members.shipping_name is 'shipping name of member';
comment on column members.shipping_zip is 'shipping zipcode of member';
comment on column members.shipping_country_id is 'shipping country id of member';
comment on column members.shipping_county_id is 'shipping county id of member';
comment on column members.shipping_city is 'shipping city of member';
comment on column members.shipping_address is 'shipping address of member';

comment on column members.contact_name is 'contact name of member';
comment on column members.contact_email is 'contact email of member';
-- comment on column members.contact_phone is 'contact phone of member';

comment on column members.haspicture is 'has picture flag of member';

comment on column members.notifications is 'type of notifications received by email (no/mylangs/mypairs/all)';
comment on column members.status_senttstamp is 'timestamp of last status notification email sent';
comment on column members.paid_tstamp is 'last payment timestamp';
comment on column members.expires_tstamp is 'expiration timestamp';

comment on column members.custom_language is 'custom language of member';
comment on column members.custom_membership is 'custom membership of member';
comment on column members.npublic_comment is 'not public comment';

comment on column members.is_deleted is 'deleted flag of member';
comment on column members.is_dirty is 'is member dirty flag';

comment on column members.umod_tstamp is 'user modified member page - timestamp';
comment on column members.amod_tstamp is 'admin modified member page - timestamp';
comment on column members.ipaddress is 'ipaddress of the last member login';
comment on column members.useragent is 'browser agent of the last member login';
comment on column members.capacity is 'capacity of user';
comment on column members.forditmail is 'forditmail flag of member';

comment on column members.custom_membership is 'custom membership';

comment on column members.oldid is 'old id';


comment on constraint members_member_id_pkey on members is 'unique constraint of table';
comment on constraint members_country_id_fkey on members is
	'foreign constraint to countries table (on country_id field)';
comment on constraint members_county_id_fkey on members is
	'foreign constraint to counties table (on county_id field)';
comment on constraint members_billing_country_id_fkey on members is
	'foreign constraint to countries table (on billing_country_id field)';
comment on constraint members_billing_county_id_fkey on members is
	'foreign constraint to counties table (on billing_county_id field)';
comment on constraint members_shipping_country_id_fkey on members is
	'foreign constraint to countries table (on shipping_country_id field)';
comment on constraint members_shipping_county_id_fkey on members is
	'foreign constraint to counties table (on shipping_county_id field)';
comment on constraint members_user_id_fkey on members is
	'foreign constraint to users table';

create index members_current_status_ind on members( current_status );
comment on index members_current_status_ind is 'status index of member';

create index members_user_id_ind on members( user_id );
comment on index members_user_id_ind is 'reference index to users table';

create index members_is_dirty_ind on members( is_dirty );
comment on index members_is_dirty_ind is 'index on is_dirty flag';

create index members_oldid_ind on members( oldid );
comment on index members_oldid_ind is 'temporary oldid index';

create index members_expires_date_ind on members( date( expires_tstamp ) );
comment on index members_expires_date_ind is 'index on expire date (truncated expires_tstamp)';
