using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using System.Reflection;
using Castle.ActiveRecord.Framework.Config;

namespace Inheritance1Example
{
    [ActiveRecord( DiscriminatorColumn="Typ", DiscriminatorType="String", DiscriminatorValue="Firma")]
    public class Firma : ActiveRecordBase
    {
        [PrimaryKey]
        public int ID { get; set; }
        [Property]
        public string Nazwa { get; set; }
    }

    [ActiveRecord( DiscriminatorValue="BiuroRachunkowe" )]
    public class BiuroRachunkowe : Firma
    {
        public BiuroRachunkowe() { L_Klient = new List<Klient>(); }
        [HasMany( typeof( Klient ), ColumnKey="ID_Klient", Cascade=ManyRelationCascadeEnum.SaveUpdate)]
        public IList<Klient> L_Klient { get; set; }
    }

    [ActiveRecord( DiscriminatorValue="Klient" )]
    public class Klient : Firma
    {
        [BelongsTo( "ID_Klient" )]
        public BiuroRachunkowe BiuroRachunkowe { get; set; }
    }
    
    class Program
    {
        static void Main( string[] args )
        {
            IConfigurationSource source = ActiveRecordSectionHandler.Instance;
            ActiveRecordStarter.Initialize( Assembly.GetAssembly( typeof( Program ) ), source );
            ActiveRecordStarter.DropSchema();
            ActiveRecordStarter.CreateSchema();

            BiuroRachunkowe b1 = new BiuroRachunkowe { Nazwa = "Ksiegowosc1" };
            BiuroRachunkowe b2 = new BiuroRachunkowe { Nazwa = "Ksiegowosc2" };

            Klient k1 = new Klient { Nazwa = "Klient1" };
            Klient k2 = new Klient { Nazwa = "Klient2" };
            Klient k3 = new Klient { Nazwa = "Klient3" };
            Klient k4 = new Klient { Nazwa = "Klient4" };
            Klient k5 = new Klient { Nazwa = "Klient5" };

            b1.L_Klient.Add( k1 );
            b1.L_Klient.Add( k2 );
            b1.L_Klient.Add( k3 );
            b2.L_Klient.Add( k4 );
            b2.L_Klient.Add( k5 );

            b1.Save(); b2.Save();
        }
    }
}
